当我向具有"授权"属性的httppost控制器方法发布表单时,它会失败,我会收到一个不好的请求。如果我从该方法中删除了" varyateantiforgerytoken"属性,则可以正常工作!这是正常的行为吗?
我的创业:
public void ConfigureServices(IServiceCollection services)
{
try
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
options.ExpireTimeSpan = new TimeSpan(90, 0, 0, 0);
options.LoginPath = new PathString("/Home/Index/");
options.AccessDeniedPath = new PathString("/Home/Index/");
options.LogoutPath = new PathString("/Home/Index/");
options.Validate();
});
services.AddMvc();
services.AddAntiforgery();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
}
catch (Exception ex)
{
gFunc.ProcessError(ex);
}
}
控制器标志:
// create claims
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, c_signed_in.FirstName + gFunc.SPACE + c_signed_in.FamilyName),
new Claim(ClaimTypes.Email, c_signed_in.Email),
// new Claim(ClaimTypes.SerialNumber, c_signed_in.AccountPassword)
};
// create identity
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); // cookie or local
// create principal
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme));
// sign-in
await HttpContext.SignInAsync(scheme: CookieAuthenticationDefaults.AuthenticationScheme, principal: principal);
我的控制器:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
我的观点:
<form class="form-group" asp-controller="Home" asp-action="SubmitAccountForm" enctype="multipart/form-data" method="post">
@Html.AntiForgeryToken()
@*code here*@
<div class="text-center">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
我相信您的最后一步是让您的应用程序在HTTP请求管道中创建XSRF令牌。此时,您的应用程序已被告知要针对XSRF进行验证,但是将NO Doken插入了服务器的请求中进行比较。
在您的startup.cs中,添加 iantiforgery Antifergery 参数,然后在下面的 XSRF Config 块。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
// XSRF config
app.Use(next => context =>
{
string path = context.Request.Path.Value;
if (
string.Equals(path, "/", StringComparison.OrdinalIgnoreCase) ||
path.StartsWith("/Login",StringComparison.OrdinalIgnoreCase))
{
// The request token can be sent as a JavaScript-readable cookie,
// and Angular uses it by default.
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
new CookieOptions() { HttpOnly = false });
}
return next(context);
});
// End XSRF config
...
注意:if语句简单地控制了为创建XSRF令牌的请求路径。您只需要确保在需要之前就可以创建它。
(我添加了"/登录"案例,因为当用户登录时,他们仍处于我应用程序中的A/登录路径,我需要XSRF令牌才能在尝试再次登录之前进行刷新。(
(配置具有iNtiforgery的抗试验特征
新的形式标签助手将反伪造令牌输出作为来自的一部分。(请参阅以下(https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms
也许可以这样尝试(编辑以显示缺失的代码(
<form class="form-group" asp-controller="Home" asp-action="SubmitAccountForm" enctype="multipart/form-data" method="post">
@*code here*@
<div class="text-center">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
我花了更多时间研究ASP.NET MVC,还购买了一本出色的书,我建议这本书:freeman撰写的" Pro ASP.NET Core MVC 2"。经过大量的挖掘,似乎您无法在邮政方法中使用[授权]属性中的[valiantiforgerytoken]属性。它会引发例外。我猜如果已授权,则无需验证令牌。