User.Identity.IsAuthenticated在重定向和验证后返回false

时间:2013-01-31 02:01:56

标签: asp.net-mvc asp.net-mvc-4

我遇到MVC4用户授权问题。

System.Web.Security.Membership.ValidateUser返回true.

然后它到达FormsAuthentication.SetAuthCookie,我在浏览器中看到一个cookie。

然后由于某种原因,User.Identity.IsAuthenticated仍然评估为false

User.Identity.IsAuthenticated
重定向后

仍为false并保持false.

model.UserName
当我致电null or empty时,

不是SetAuthCookie。它具有正确的用户名。

表单身份验证在web.config.

中指定
[AllowAnonymous]
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (System.Web.Security.Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

1 个答案:

答案 0 :(得分:1)

另请参阅 System.Threading.Thread.CurrentPrincipal 你应该看到

  • AuthenticationType“Forms”
  • IsAuthenticated true
  • 姓名“fred”

您是否告诉MVC / IIS授权您的控制器? 例如

[Authorize]
public class MyController : Controller

如果未设置 User.Identity ,则会建议缺少WEB.CONFIG或显式的AUTHORIZE属性。