asp.net-core-mvc – Html.AntiForgeryToken()仍然需要?

在ASP.NET .NET4.6 vNext中仍然需要@ Html.AntiForgeryToken()?

表单装饰已更改为

<form asp-controller="Account" 
      asp-action="Login" 
      asp-route-returnurl="@ViewBag.ReturnUrl" 
      method="post" 
      class="form-horizontal" 
      role="form">

由此

@using (Html.BeginForm("Login","Account",new { ReturnUrl = ViewBag.ReturnUrl },FormMethod.Post,new { @class = "",role = "form" }))

不再包含这个

@Html.AntiForgeryToken()

控制器操作仍然按照预期标记有ValidateAntiForgeryToken属性,但是它来自哪里?自动的?

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(Loginviewmodel model,string returnUrl = null)

解决方法

表单标签助手将自动添加防伪令牌. (除非您将其用作标准的html表单元素,手动添加操作属性).检查 form tag helper的源代码,您将在Process方法的末尾看到以下内容.
if (Antiforgery ?? antiforgeryDefault)
{
    var antiforgeryTag = Generator.GenerateAntiforgery(ViewContext);
    if (antiforgeryTag != null)
    {
        output.PostContent.AppendHtml(antiforgeryTag);
    }
}

如果您检查登录页面的html,您将在窗体中看到以下隐藏的输入:

<input name="__RequestVerificationToken" type="hidden" value="CfDJ8BIeHClDdT9...">

您还可以手动启用/禁用它添加了asp-antimorgery属性

<form asp-controller="Account" asp-action="Register" asp-antiforgery="false" method="post" class="form-horizontal" role="form">

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...