Asp.net mvc 4 为什么User.Identity.IsAuthenticated始终为false?
我在为这个“愚蠢”的问题绞尽脑汁。我尝试查看用户在我的操作中是否经过身份验证。我习惯于像Asp.net mvc 4 为什么User.Identity.IsAuthenticated始终为false?,asp.net-mvc-4,Asp.net Mvc 4,我在为这个“愚蠢”的问题绞尽脑汁。我尝试查看用户在我的操作中是否经过身份验证。我习惯于像HttpContext.User.Identity.IsAuthenticated这样做,但它总是返回false。我查看authorized属性的代码,它们使用 protected virtual bool AuthorizeCore(HttpContextBase httpContext) { //removed code... IPrinc
HttpContext.User.Identity.IsAuthenticated这样做,但它总是返回false。我查看authorized属性的代码,它们使用
protected virtual bool AuthorizeCore(HttpContextBase httpContext)
{
//removed code...
IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated)
{
return false;
}
//removed code....
}
所以,我的问题和困惑是,为什么这在我的行动中不起作用
我使用Simplemembership,这是我在web.config
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider"
type="WebMatrix.WebData.SimpleRoleProvider,
WebMatrix.WebData" />
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider"
type="WebMatrix.WebData.SimpleMembershipProvider,
WebMatrix.WebData" />
</providers>
</membership>
我在我的authorizationattribute中使用了它,它是有效的,所以我的问题是有什么区别
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
//code here....
if (httpContext.User.Identity.IsAuthenticated)
return true;
return true;
}
这就是我在行动中所使用的
public ActionResult Index()
{
//removed code....
if (HttpContext.User.Identity.IsAuthenticated)
{
return View();
}
// revmoved code...
return View();
}
我通常使用应用程序\u PostAuthenticateRequest
来进行此操作,而不是筛选。
请随意忽略我的CustomPrincipal
,您不必这样做。只是从我的一个项目快速粘贴
我没有看到您的表单验证代码
很难说为什么HttpContext。用户设置不正确。请将您的表格张贴在身份验证
代码上
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
var serializeModel = (CustomPrincipalSerializeModel)
JsonSerializer.DeserializeFromString(authTicket.UserData,
typeof (CustomPrincipalSerializeModel));
var roles = serializeModel.Roles ?? new string[0];
var newUser = new CustomPrincipal(authTicket.Name, roles)
{
UserId = serializeModel.UserId,
UserName = serializeModel.UserName,
FirstName = serializeModel.FirstName,
LastName = serializeModel.LastName
};
HttpContext.Current.User = newUser;
}
}
如果不知道您的身份验证设置(可能是在web.config中设置的),则很难回答此问题。身份验证cookie尚未设置。请看@Jasen,我没有看到他的登录方法来确认他是否设置了身份验证cookie,甚至没有使用cookie。您看到了什么?请共享您的登录名method@Jasen看看我的问题,我更新了。我只是不明白为什么它在属性中工作而不使用cookies?
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
var serializeModel = (CustomPrincipalSerializeModel)
JsonSerializer.DeserializeFromString(authTicket.UserData,
typeof (CustomPrincipalSerializeModel));
var roles = serializeModel.Roles ?? new string[0];
var newUser = new CustomPrincipal(authTicket.Name, roles)
{
UserId = serializeModel.UserId,
UserName = serializeModel.UserName,
FirstName = serializeModel.FirstName,
LastName = serializeModel.LastName
};
HttpContext.Current.User = newUser;
}
}