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