C# 当cookie设置为httponly时,无法验证反伪造令牌

C# 当cookie设置为httponly时,无法验证反伪造令牌,c#,angular,asp.net-core,antiforgerytoken,cookie-httponly,C#,Angular,Asp.net Core,Antiforgerytoken,Cookie Httponly,当我设置httponly=true的值时。AutoValidateAntiforgeryTokenAttribute不验证请求并返回400个错误请求 AntiforgeryTokenSet tokens = antiforgery.GetAndStoreTokens(context); context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, new CookieOptions() { HttpOnly =

当我设置httponly=true的值时。AutoValidateAntiforgeryTokenAttribute不验证请求并返回400个错误请求

AntiforgeryTokenSet tokens = antiforgery.GetAndStoreTokens(context);
   context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
    new CookieOptions() { HttpOnly = true});
执行此操作后,cookie将变得安全,如下所示

设置cookie:XSRF-TOKEN={{cookie值};路径=/;samesite=lax;httponly

但AutoValidateAntiforgeryTokenAttribute返回400个错误请求

AntiforgeryTokenSet tokens = antiforgery.GetAndStoreTokens(context);
   context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
    new CookieOptions() { HttpOnly = true});
将值HttpOnly=true更改为HttpOnly=false时


它工作得非常好。是否有办法在asp core和angular 7中将antiforgerytoken cookie设置为httponly。如果它不起作用,请有人告诉我它背后的原因。

不,这个特定的cookie不可能只设置为http。要了解原因,请查看ASP.NET Core中的以下部分:

AngularJS使用约定来处理CSRF。如果服务器发送名为
XSRF-TOKEN
的cookie,则AngularJS
$http
服务在向服务器发送请求时会将cookie值添加到头中

这涉及AngularJS和
$http
,但Angular及其
HttpClient
服务使用相同的机制(参见Angular)

为了能够将cookie值添加到标头,Angular必须能够从cookie中读取值。当您将cookie设置为
HttpOnly
时,Angular无法从cookie中读取值(就好像它根本不存在一样)。因此,不会发送该值,服务器会拒绝该请求