[ValidateAntiForgeryToken] public class LoginModel : PageModel
<el-form style="max-width:600px; margin:20px auto;" method="post"> @Html.AntiForgeryToken()
$.ajaxSetup({ beforeSend: function (xhr) { xhr.setRequestHeader("RequestVerificationToken", $('input:hidden[name="__RequestVerificationToken"]').val()); } })
(有時候這種安全性是沒必要須的,好比不是在頁面裏發起請求,須要採用其它安全機制)html
//在 public void ConfigureServices(IServiceCollection services) 方法裏: services.AddMvc() .AddRazorPagesOptions(o => { o.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }) .InitializeTagHelper<FormTagHelper>((helper, context) => helper.Antiforgery = false) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);