我正在使用
需要反序列化的信息如下:
- 用户ID(int) - 角色(string[]) - 电子邮件(string) - 相关ID(int[])//(很难描述这些是什么,但每个用户将拥有约3个)
实现自定义
global.asax
文件中的Application_PostAuthenticateRequest
方法(ASP.NET MVC)实现自定义工单系统。我想知道这种方法的开销是多少,因为它会在每个请求上反序列化一些信息。它生成了一个大约1.8 kb的cookie,这很多 - 但是这比频繁地访问数据库更好吗?需要反序列化的信息如下:
- 用户ID(int) - 角色(string[]) - 电子邮件(string) - 相关ID(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;
}
}
}
这里的类被序列化为UserData
,在FormsAuthenticationTicket
中使用。
[Serializable]
public class MembershipData
{
public string Email
{
get;
set;
}
public int Id
{
get;
set;
}
public string[] Roles
{
get;
set;
}
public int[] Ancillary
{
get;
set;
}
}