3

当我们在同一个 cshtml 页面上有多个可能的表单发布到控制器时,Antiforgery 验证不起作用。我们浏览了 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" />
}

你能帮我解决这个问题吗?我们发现,在移除其中一个防伪令牌后,一切似乎都按预期工作。

我们尝试为防伪设置机器密钥,但它也不起作用。

问候。

4

1 回答 1

6

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

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

然后,在我需要令牌的每种形式中,我添加以下内容:

@Html.Raw(ViewBag.__RequestVerificationToken)
于 2011-11-25T16:32:59.503 回答