【问题标题】:AntiForgeryToken Error in ASP.NET MVC 5 appASP.NET MVC 5 应用程序中的 AntiForgeryToken 错误
【发布时间】:2016-10-15 02:53:06
【问题描述】:

我收到此错误 - 即使 AntiForgeryToken 在我看来肯定是在表单标签内:

所需的防伪cookie “__RequestVerificationToken_L0NpdTpLaW5nMTZNVkM10”不存在。

控制器

/// <summary>
/// Delete 
/// </summary>
public ActionResult Delete(int Id)
{
    // Get place from Id
    var poll = PollRepo.Select(Id);

    if (poll == null)
        return HttpNotFound();

    return View(poll);
}

/// <summary>
/// Confirm Delete
/// </summary>
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int Id)
{
    // Delete poll by Id from db
    PollRepo.Delete(Id);

    // Redirect to index
    TempData["message"] = "Poll Deleted";
    return RedirectToAction("Index");
}

查看

    <dd>
        @Html.DisplayFor(model => model.Abc)
    </dd>

</dl>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-actions">
        <input type="submit" value="Delete" class="btn btn-default" /> |
        @Html.ActionLink("Back to List", "Index")
    </div>
}

在生成的 HTML 页面中

<form action="/MyApp/MyCont/MyAct/Delete/7" method="post"><input name="__RequestVerificationToken" type="hidden" value="JYMlRqNTUF6eoagnN6k7GrC1mJLKs1HDU4RCY_5_MEh2sIoJtumYEiM4LQF2BcKrf881xm-zdRU-KwBt381L9vBhuEJRLnMJY8aEgjVvdd41" /> 

当我按下删除按钮时,错误被返回。

【问题讨论】:

  • 该消息是指防伪cookie所以这是由于cookie不存在
  • 一个可能的原因是&lt;httpCookies requireSSL="true" /&gt;web.config.cs 文件中,但项目未设置为使用SSL。
  • @StephenMuecke 这是 requireSSL 谢谢!服务器需要 SSL - 这个标志也被错误地设置在开发机器上。如果你把它转换成答案,我会接受它。
  • 我在 SO 上发现的关于这个错误的信息很少,但我认为还有其他原因可能导致它,如果我发现任何东西,我稍后会更新答案。否则,我会将其标记为社区 wiki,以便其他人可以根据其他可能的原因对其进行编辑。

标签: asp.net-mvc asp.net-mvc-5


【解决方案1】:

您看到的错误消息与防伪 cookie 有关,而不是令牌(您显示的代码将在请求中正确提交令牌)。

除了来自恶意用户的攻击或客户端上的某些东西导致 cookie 被删除之外,此错误的一个原因是您的 web.config.cs 文件包含

<httpCookies requireSSL="true" />

但您的项目未设置为使用 SSL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多