1

当我的用户单击登录并通过身份验证时,我将他们重定向到此 ActionResult。它把他们带到了这个方法:

[Authorize]
private ActionResult RouteRegistrationStep()
{
    Debug.Print(HttpContext.User.Identity.IsAuthenticated.ToString()); // false
    Debug.Print(HttpContext.User.Identity.Name); // blank
    return RedirectToAction("ContactInfo", "Adjuster");
}

怎么是HttpContext.User.Identity.IsAuthenticated.ToString()假的?而且,如果它是假的,为什么[Authorize]属性让它在方法开始?

编辑:

这是将它们重定向到的登录方法RouteRegistrationStep()

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && db.Users.Where(x => x.username == model.username 
        && x.password == EncryptPassword(model.password)).Count() > 0)
    {
        FormsAuthentication.SetAuthCookie(model.username, model.RememberMe);
        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

[Authorize]
private ActionResult RedirectToLocal(string returnUrl)
{
    if (Url.IsLocalUrl(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RouteRegistrationStep();
    }
}
4

1 回答 1

2

操作过滤器仅适用于公共操作方法,而不适用于私有操作方法。

此外,该FormsAuthentication.SetAuthCookie方法将 cookie 写入 HTTP 响应,直到下一个请求才可用。您需要在用户获得授权之前进行重定向。来自MSDN

forms-authentication ticket 为浏览器发出的下一个请求提供表单认证信息。

因此,在设置 auth cookie 后,将用户重定向到另一个具有该Authorize属性的操作,它应该可以正常工作。

于 2013-05-03T13:03:14.527 回答