我正在尝试将角色分配给日志记录用户,但是它不起作用。
我尝试用用户角色创建表单身份验证票证,但是它不起作用
控制器代码:
var userinfo = opidb.user_info.Where(a => a.user_email.Equals(mail)).FirstOrDefault();
var userroleinfo = opidb.user_role.Where(a => a.role_id == userinfo.user_role).FirstOrDefault();
var authticket = new FormsAuthenticationTicket(1, userinfo.user_id.ToString(), DateTime.Now, DateTime.Now.AddMinutes(10), false, "Admin");
string encryptedticket = FormsAuthentication.Encrypt(authticket);
var authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedticket);
System.Web.HttpContext.Current.Response.Cookies.Add(authcookie);
FormsAuthentication.SetAuthCookie(x.user_name,false);
Global.asax.cs代码:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == "")
return;
FormsAuthenticationTicket authTicket;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch
{
return;
}
// retrieve roles from UserData
string[] roles = authTicket.UserData.Split(';');
if (Context.User != null)
Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
我希望代码应将角色分配给用户,并基于此我可以将用户限制在不同的区域。