public static void SetCurrentHttpContextUser()
{
HttpApplication app = HttpContext.Current.ApplicationInstance;
IPrincipal requestUser = app.User;
if (requestUser != null)
{
string ticketStr = app.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(ticketStr);
string[] roles = ticket.UserData.Split(',');
HttpContext.Current.User = new GenericPrincipal(requestUser.Identity, roles);
}
}
{
HttpApplication app = HttpContext.Current.ApplicationInstance;
IPrincipal requestUser = app.User;
if (requestUser != null)
{
string ticketStr = app.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(ticketStr);
string[] roles = ticket.UserData.Split(',');
HttpContext.Current.User = new GenericPrincipal(requestUser.Identity, roles);
}
}
该代码是获得当前用户的名字和角色requestUser != null
登陆用户不为空string ticketStr = app.Request.Cookies[FormsAuthentication.FormsCookieName].Value;
取出Cookies中的用户名string[] roles = ticket.UserData.Split(',');
取出当前用户的所有角色,是个数组