MVC 4提供的防伪令牌是针对用户“”但当前用户是“用户”

时间:2013-12-02 11:17:37

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

我最近将Live使用 MVC 4 Entity Framework 5 构建的Web应用程序。 MVC 应用程序使用 Razor Views

我注意到使用 Elmah ,当用户登录应用程序时,有时会出现以下错误

The provided anti-forgery token was meant for user "" but the current user is "user"

我已经就如何解决这个问题做了一些研究,但似乎没有什么对我有用。请参阅我的登录视图以及相应的控制器操作

Razor查看

@if (!HttpContext.Current.User.Identity.IsAuthenticated)
{

using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

     <div class="formEl_a">

        <fieldset>
            <legend>Login Information</legend>

            <div class="lbl_a">
                Email
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.Email, new { @class = "inpt_a" })<br />
                @Html.ValidationMessageFor(m => m.Email)
            </div>

            <div class="lbl_a">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field sepH_b">
                @Html.PasswordFor(m => m.Password, new { @class = "inpt_a" })<br />
                @Html.ValidationMessageFor(m => m.Password)
            </div>


        </fieldset>
    </div>
    <br />
      <p>
            <input type="submit" value="Log In" class="btn btn_d sepV_a" />
        </p>

}    
}

控制器

[AllowAnonymous]
public ActionResult Login()
{
     return View();
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
     if (ModelState.IsValid && _accountService.Logon(model.Email, model.Password, true))
     {
          //Validate
     }
     else
     {
          // inform of failed login
      }

}

我觉得这一切看起来还不错,但问题仍然存在。有没有关于如何解决这个问题的想法?

非常感谢您的帮助。

感谢。

3 个答案:

答案 0 :(得分:17)

我相信这是因为用户双击表单上的提交按钮。至少在我的网站上确实如此。

Troubleshooting anti-forgery token problems

答案 1 :(得分:11)

针对AntiForgeryToken运行的验证代码还会检查您登录的用户凭据是否未更改 - 这些也在Cookie中加密。 这意味着,如果您在弹出窗口或其他浏览器选项卡中登录或注销,则表单提交将失败,并出现以下异常:

System.Web.Mvc.HttpAntiForgeryException (0x80004005):
The provided anti-forgery token was meant for user "", but the current user is "SomeOne".

您可以将AntiForgeryConfig.SuppressIdentityHeuristicChecks = true;方法放在Application_Start文件中,将其关闭。

当AntiForgeryToken未验证您的网站时,将抛出类型为Global.asax的异常。您可以通过捕获HttpAntiForgeryException,至少为用户提供针对这些异常的信息更丰富的页面,从而使这更容易。

System.Web.Mvc.HttpAntiForgeryException

更多信息:

Anti forgery token is meant for user “” but the current user is “username”

Html.AntiForgeryToken – Balancing Security with Usability

答案 2 :(得分:0)

时遇到同样的问题
  • 用户登录
  • 然后在主页上,用户点击后退按钮返回登录
  • 用户以其他用户身份登录
  • 这给出了例外:提供的防伪令牌适用于用户“”,但当前用户是“用户”

我发现这只发生在IE中,我通过做几件事来修复它

  1. 已禁用登录页面的输出缓存,因为在调试模式下,我发现点击后退按钮未向登录页面生成新请求
  2. 在登录页面上,我添加了一项检查,以查看用户是否已经过身份验证,如果是,则注销用户,然后再次重定向到“登录”页面。

    [AllowAnonymous]
    [OutputCache(NoStore=true, Location=System.Web.UI.OutputCacheLocation.None)]
    public ActionResult Login)
    {
        if (HttpContext.Request.IsAuthenticated)
        {
            WebSecurity.Logout();
            Session.Abandon();
            return RedirectToAction("Login");
        }
    
        return View();
    }
    
相关问题