![](/img/trans.png)
[英]Why does System.Web.HttpContext.Current.User resolve during a controller constructor, but User.Identity does not
[英]MVC4 Custom Forms Authentication - Unable to cast System.Web.HttpContext.Current.User as CustomPrincipal
我正在.Net MVC4中建立网站,并尝试使用CustomPrincipal对象实施自己的表单身份验证。
这是我的登录方法...
public void Login(string email)
{
var user = _userManager.GetUserByEmail(email);
var encryptedCookieContent = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, user.Email, DateTime.Now, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false, user.Id + "|" + user.AccountId + "|" + user.RoleId + "|" + user.Role.Name, FormsAuthentication.FormsCookiePath));
var formsAuthenticationTicketCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookieContent)
{
Domain = FormsAuthentication.CookieDomain,
Path = FormsAuthentication.FormsCookiePath,
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL
};
HttpContext.Current.Response.Cookies.Add(formsAuthenticationTicketCookie);
}
这是我的自定义校长
public class CustomPrincipal : IPrincipal
{
public CustomPrincipal(IIdentity identity, int userId, int accountId, string role, int roleId)
{
Identity = identity;
Role = role;
RoleId = roleId;
UserId = userId;
AccountId = accountId;
}
public bool IsInRole(string role)
{
return Role == role;
}
public bool IsInRole(int roleId)
{
return RoleId == roleId;
}
public IIdentity Identity { get; private set; }
public string Role { get; private set; }
public int RoleId { get; private set; }
public int UserId { get; private set; }
public int AccountId { get; private set; }
}
我没有实现Identity的自定义实例,而是在重用GenericIdentity。 在我的Global.asax文件中,我像这样安装了Application_AuthenticateRequest方法...
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
var formsCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (formsCookie != null)
{
var ticket = FormsAuthentication.Decrypt(formsCookie.Value);
var principal = PrincipalConfig.CreatePrincipal(ticket);
System.Web.HttpContext.Current.User = Thread.CurrentPrincipal = principal;
}
}
}
PrincipalConfig.CreatePrincipal是将我的自定义主体对象放在一起的方法。 这基本上只是填写userData ...这样的...
public static CustomPrincipal CreatePrincipal(FormsAuthenticationTicket ticket)
{
var userData = ticket.UserData.Split('|');
var principal = new CustomPrincipal(new GenericIdentity(ticket.Name), Convert.ToInt32(userData[0]), Convert.ToInt32(userData[1]), userData[3], Convert.ToInt32(userData[2]));
return principal;
}
因此,现在回头参考global.asax文件中的Application_AuthenticateRequest,您可以看到我正在使用新创建的CustomPrincipal对象并将其分配给System.Web.HttpContext.Current.User。
这样做之后,如果我仍然在Application_AuthenticateRequest方法中,但在即时窗口中放置了以下内容...
System.Web.HttpContext.Current.User as CustomPrincipal
就像我期望的那样,向我显示了CustomPrincipal对象中的所有数据。
但是,如果稍后(在其他一些控制器操作中)尝试访问System.Web.HttpContext.Current.User并将其强制转换为CustomPrincipal,它将返回null。
再次,对于应用程序中发出的每个请求,我都可以从Application_AuthenticateRequest方法内部的FormsAuthentication cookie解密FormsAuthenticationTicket,然后找到我的所有UserData。 因此,似乎cookie已被创建并正确读取。 但是,当将新的CustomPrincipal分配给System.Web.HttpContext.Current.User时,从那时到我尝试在另一个控制器内再次访问它时,似乎存在某种断开连接。
有人可以在这里看到我做错了吗?
让我知道是否需要澄清任何事情。
谢谢
我不确定你在用
Request.IsAuthenticated
正确。
Request.IsAuthenticated属性本质上返回与Context.User.Identity.IsAuthenticated属性相同的值。 在您的代码中,它应始终返回false,因此分配自定义主体的代码将永远不会触发。
您可以通过在控制器方法中设置断点并检查代码中Context.Current.User的类型来对其进行调试。 是GenericIdentity吗? 如果是这样,则永远不会分配您的校长。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.