【问题标题】:MVC 4 provided anti-forgery token was meant for user "" but the current user is "user"MVC 4 提供的防伪令牌用于用户“”,但当前用户是“用户”
【发布时间】:2013-12-18 02:00:57
【问题描述】:

我最近发布了一个使用 MVC 4Entity Framework 5 构建的 Web 应用程序 Live。 MVC 应用程序使用 Razor 视图

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

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

我已经对如何解决此问题进行了一些研究,但似乎没有什么对我有用。请在下面查看我的登录视图和相应的控制器操作

剃刀视图

@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
      }

}

我认为这一切看起来都不错,但错误仍然存​​在。有没有人对如何解决这个问题有任何想法?

非常感谢您的帮助。

谢谢。

【问题讨论】:

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


【解决方案1】:

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

Troubleshooting anti-forgery token problems

【讨论】:

  • 这里也一样。摘自链接问题的最受欢迎答案:我刚刚删除了验证属性。我的网站始终是 SSL,我并不太担心风险。我只需要它现在就可以工作。另一种解决方案是使用 javascript 禁用按钮。
  • @Jerther:不要删除验证属性,尤其是登录页面:security.stackexchange.com/questions/2120/…
【解决方案2】:

针对 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 方法中的Global.asax 文件中来关闭此功能。

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

private void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if (ex is HttpAntiForgeryException)
    {
        Response.Clear();
        Server.ClearError(); //make sure you log the exception first
        Response.Redirect("/error/antiforgery", true);
    }
}

更多信息:

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

Html.AntiForgeryToken – Balancing Security with Usability

【讨论】:

  • AntiForgeryConfig.SuppressIdentityHeuristicChecks设置为true有什么问题吗?
  • 不,这个设置没有问题。如果用户凭据已更改,则会出现此问题。
  • 我实际上更喜欢这个解决方案,因为它保持安全检查到位并正确通知用户,不幸的是很难阻止用户双击或按下浏览器后退按钮返回登录页面或东西。
【解决方案3】:

我遇到了同样的问题

  • 用户登录
  • 然后在主页上,用户点击返回按钮返回登录
  • 用户以其他用户身份登录
  • 这给出了异常:提供的防伪令牌用于用户“”,但当前用户是“用户”

我发现这仅在 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();
    }
    

【讨论】:

    猜你喜欢
    • 2016-03-19
    • 2015-02-01
    • 2013-02-04
    • 2013-04-30
    • 2013-10-06
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多