【问题标题】:How can I use multiple ajax forms with AntiForgery validation on the same MVC3 page?如何在同一个 MVC3 页面上使用多个带有 AntiForgery 验证的 ajax 表单?
【发布时间】:2012-01-06 10:49:01
【问题描述】:

当我们在同一个 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" />
}

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

我们尝试为防伪设置机器密钥,但也没有用。

问候。

【问题讨论】:

    标签: ajax asp.net-mvc-3 razor antiforgerytoken


    【解决方案1】:

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

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

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

    @Html.Raw(ViewBag.__RequestVerificationToken)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      相关资源
      最近更新 更多