【问题标题】:How can I fix anti-forgery token was meant for user "", but the current user is "xxxx " error如何修复防伪令牌适用于用户“”,但当前用户是“xxxx”错误
【发布时间】:2015-01-14 17:35:56
【问题描述】:

提供的防伪令牌用于用户“”,但当前用户是“xxxx”。

我已遵循所有可能的解决方案来消除此错误,但没有成功:

这是场景: 我在浏览器选项卡 A 选项卡 B 中打开了 2 个单独的登录选项卡: 1. 我在 Tab A 中登录我的网站 2.然后尝试登录Tab B

然后出现上述错误

我的 C# web MVC 登录视图有:

v class="col-md-4 col-md-offset-4">
                <form class="form-signin" role="form" action=@Url.Action("Login", "Account") method="POST" id="signInForm">
                     @Html.AntiForgeryToken()
                    <div class="form-group">
                        <label for="loginEmail">Email</label>
                        <input  type="text" id="loginEmail" name="loginEmail" class="form-control" placeholder="Email address" >
                    </div>
                    <div class="form-group">
                        <label for="loginPassword">Password</label>
                        <input id="loginPassword" type="password"  name="loginPassword" class="form-control" placeholder="Password" >
                    </div>
                    <button class="btn btn-lg btn-primary btn-block main-btn" type="submit">Sign in</button>
                    <div>
                        <br>
                        <a href="@Url.Action("Index","GettingStarted")">Register</a><br>
                        <a href="@Url.Action("ForgotPassword","Account")">Forgot your password?</a>
                    </div>
                </form>

我的帐户控制器是这样的:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{

    if (ModelState.IsValid)
    {

我该如何解决这个错误?

【问题讨论】:

  • 它不在——至少不在我的窗口中(也刷新了)
  • 这在现实世界中什么时候会成为一个问题(这意味着一个人最终如何尝试从两个选项卡登录两次到同一个站点)?一旦你在 Tab A 上登录,你就在 Tab B 上登录,只需刷新页面...

标签: c# asp.net-mvc


【解决方案1】:

这是因为两个浏览器标签共享同一个 cookie 存储。使用第一个选项卡进行身份验证会设置一个新的 cookie,用于标识您的用户名。提交第二个选项卡时,它将传递从第一个选项卡中的成功身份验证中检索到的更新 cookie,以及在身份验证之前加载的隐藏表单字段,该字段将您标识为匿名用户。因为cookie中的用户名和隐藏的表单域不匹配验证失败。

ValidateAntiForgeryTokenAttribute 使用的 AntiForgeryWorker 将经过身份验证的用户名编码到 cookie 和隐藏的表单字段中,并确保它们在验证时都匹配。因此,每当您进行身份验证或更改用户时,如果发布到具有 ValidateAntiForgeryTokenAttribute 的操作,此验证将失败。

不幸的是,这意味着您的选项仅限于不使用 ValidateAntiForgeryTokenAttribute 保护登录操作、忽略您描述的场景并让验证失败,或者在 MVC 中重新实现 AntiForgery 实现,以便在验证过程中不包括用户名。

【讨论】:

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