{
var newTicket = new FormsAuthenticationTicket(1,
memberId.ToString(),
DateTime.Now,
DateTime.Now.AddMinutes(30),
false,
accountType,
FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(newTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
HttpContext.Current.Response.Cookies.Add(cookie);
}
"UserData"这里为 accountType. 不能为Null 否则 Encrypt的时候会为null.
//Global.asax
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket != null && authTicket.UserData != null)
{
var rehauIdentity = new RehauIdentity(HttpContext.Current.User.Identity) {AccountType = authTicket.UserData};
HttpContext.Current.User = rehauIdentity;
}
}
}
//IdentityClass
public class RehauIdentity : IPrincipal
{
public IIdentity Identity { get; private set; }
public bool IsInRole(string role) { return Roles.IsUserInRole(Identity.Name, role); }
public RehauIdentity(IIdentity identity) { this.Identity = identity; }
public string AccountType { set; get; }
}