如何在同一个 MVC3 页面上使用多个具有 AntiForgery 验证的 ajax 表单?
当我们有不止一种可能的表单可以发布到同一 cshtml 页面上的控制器时, 防伪验证不起作用。我们检查了 MVC3 代码,发现问题出在这部分代码中:
if (!String.Equals(cookieToken.Value, formToken.Value, StringComparison.Ordinal)) {
// error: form token does not match cookie token
throw CreateValidationException();
}
我们拥有的 cshtml 是这样的:
@using (@Ajax.BeginForm()) {
@Html.AntiForgeryToken()
<input type="submit" class="buttonBlue" value="form1" />
}
@using (@Ajax.BeginForm()) {
@Html.AntiForgeryToken()
<input type="submit" class="buttonBlue" value="form2" />
}
你能帮我解决这个问题吗?我们发现,在删除其中一个防伪令牌后,一切似乎都按预期工作。
我们尝试设置防伪机器密钥,但也不起作用。
问候。
When we have more than one possible form to post to the controller on the same cshtml page,
the Antiforgery validation does not work. We went through the MVC3 code and we found the problem is in this part of the code:
if (!String.Equals(cookieToken.Value, formToken.Value, StringComparison.Ordinal)) {
// error: form token does not match cookie token
throw CreateValidationException();
}
The cshtml that we have is something like this:
@using (@Ajax.BeginForm()) {
@Html.AntiForgeryToken()
<input type="submit" class="buttonBlue" value="form1" />
}
@using (@Ajax.BeginForm()) {
@Html.AntiForgeryToken()
<input type="submit" class="buttonBlue" value="form2" />
}
Can you help me to fix this issue? We found that after removing one of the antiforgery tokens eveyrthing seems to work as expected.
We tried setting the machine key for the antiforgery and it didn't work either.
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了多次使用 AntiForgeryToken,我执行以下操作。首先,在视图顶部添加以下行:
然后,在需要令牌的每个表单中,添加以下内容:
In order to use the AntiForgeryToken multiple times, I do the following. First, at the top of my view, I add the following line:
Then, in each form where I need the token, I add the following: