mat*_*tt. 22 antiforgerytoken asp.net-core-mvc
是@Html.AntiForgeryToken()仍然在ASP.NET .NET4.6 vNext要求?
表单装饰已更改为
<form asp-controller="Account"
asp-action="Login"
asp-route-returnurl="@ViewBag.ReturnUrl"
method="post"
class="form-horizontal"
role="form">
Run Code Online (Sandbox Code Playgroud)
由此
@using (Html.BeginForm("Login",
"Account",
new { ReturnUrl = ViewBag.ReturnUrl },
FormMethod.Post,
new { @class = "", role = "form" }))
Run Code Online (Sandbox Code Playgroud)
而且不再包括这个
@Html.AntiForgeryToken()
Run Code Online (Sandbox Code Playgroud)
控制器动作仍然ValidateAntiForgeryToken按预期标记属性,但它究竟来自何处?自动的?
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
Run Code Online (Sandbox Code Playgroud)
Dan*_*.G. 42
表单标记助手将自动添加防伪标记.(除非您将其用作标准html表单元素,否则手动添加action属性).检查表单标记助手的源代码,您将在Process方法的末尾看到以下内容.
if (Antiforgery ?? antiforgeryDefault)
{
var antiforgeryTag = Generator.GenerateAntiforgery(ViewContext);
if (antiforgeryTag != null)
{
output.PostContent.AppendHtml(antiforgeryTag);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您检查登录页面的html,您将在表单中看到以下隐藏的输入:
<input name="__RequestVerificationToken" type="hidden" value="CfDJ8BIeHClDdT9...">
Run Code Online (Sandbox Code Playgroud)
您还可以手动启用/禁用添加asp-antiforgery属性:
<form asp-controller="Account" asp-action="Register" asp-antiforgery="false" method="post" class="form-horizontal" role="form">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18858 次 |
| 最近记录: |