【发布时间】:2011-02-09 18:54:33
【问题描述】:
我正在使用 global.asax 文件 (ASP.NET MVC) 中的 Application_PostAuthenticateRequest 方法实现自定义票证系统。我想知道这种事情的开销是什么——因为它会反序列化每个请求的一些信息。它会生成一个大约 1.8 kb 的 cookie,这是一大堆 - 但这是否比频繁访问数据库更好?
正在反序列化的信息
- 用户 ID(整数)
- 角色(字符串[])
- 电子邮件(字符串)
- Associated Ids (int[]) //(很难描述这些是什么,但每个用户大约有 3 个)
实现一个自定义的FormsAuthenticationTicket 系统似乎比基于User.Identity.Name 不断地往返于数据库。但我只是担心这种不断的反序列化非常抑制。但它看起来像这样......
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
string encTicket = authCookie.Value;
if (!String.IsNullOrEmpty(encTicket))
{
// decrypt the ticket if possible.
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
var userData = Deserializer.Deserialize(ticket);
UserPrincipal principal = new UserPrincipal(userData);
HttpContext.Current.User = principal;
}
}
}
这是在FormsAuthenticationTicket 中被序列化为UserData 的类。
[Serializable]
public class MembershipData
{
public string Email
{
get;
set;
}
public int Id
{
get;
set;
}
public string[] Roles
{
get;
set;
}
public int[] Ancillary
{
get;
set;
}
}
【问题讨论】:
-
我在野外有许多应用程序可以执行此操作 - 将数据存储在表单身份验证票证的 userData 部分。我不必反序列化,因为数据只是一个逗号分隔的列表,但我想这对你来说不会成为瓶颈。你必须做一些测试。
标签: c# performance asp.net-mvc-3 forms-authentication