如何在同一个 MVC3 页面上使用多个具有 AntiForgery 验证的 ajax 表单?

发布于 2024-12-17 22:13:36 字数 700 浏览 2 评论 0原文

当我们有不止一种可能的表单可以发布到同一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-12-24 22:13:36

为了多次使用 AntiForgeryToken,我执行以下操作。首先,在视图顶部添加以下行:

ViewBag.__RequestVerificationToken = @Html.AntiForgeryToken().ToString();

然后,在需要令牌的每个表单中,添加以下内容:

@Html.Raw(ViewBag.__RequestVerificationToken)

In order to use the AntiForgeryToken multiple times, I do the following. First, at the top of my view, I add the following line:

ViewBag.__RequestVerificationToken = @Html.AntiForgeryToken().ToString();

Then, in each form where I need the token, I add the following:

@Html.Raw(ViewBag.__RequestVerificationToken)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文