【发布时间】:2011-08-11 16:27:05
【问题描述】:
我有一个表单帖子一直给我一个防伪令牌错误。
这是我的表格:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.EditorFor(m => m.Email)
@Html.EditorFor(m => m.Birthday)
<p>
<input type="submit" id="Go" value="Go" />
</p>
}
这是我的操作方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Join(JoinViewModel model)
{
//a bunch of stuff here but it doesn't matter because it's not making it here
}
这是 web.config 中的 machineKey:
<system.web>
<machineKey validationKey="mykey" decryptionKey="myotherkey" validation="SHA1" decryption="AES" />
</system.web>
这是我得到的错误:
A required anti-forgery token was not supplied or was invalid.
我已经读到在 HttpContext 上更改用户将使令牌无效,但这里不会发生这种情况。我的 Join 操作上的 HttpGet 只返回视图:
[HttpGet]
public ActionResult Join()
{
return this.View();
}
所以我不确定发生了什么。我四处搜索,一切似乎都表明它是 machineKey 更改(应用程序周期)或用户/会话更改。
还有什么可能发生的?我该如何解决这个问题?
【问题讨论】:
-
这个页面在 18 个月内被浏览了近 4000 次,没有人知道你需要做的只是双击登录按钮来复制它?
-
双重发布是触发防伪令牌异常的一种方式。正如您从下面的代码中看到的那样,有很多不同的场景可以抛出这个,特别是在我的例子中,它与重复发布无关。
标签: asp.net-mvc asp.net-mvc-3 antiforgerytoken