微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – .Net Core中的.AspNetCore.Antiforgery.xxxxxxx cookie是什么?

我试图在.Net Core中使用ValidateAntiForgeryToken,但我得到了.AspNetCore.Antiforgery.xxxxxxx cookie丢失了.

这是什么.AspNetCore.Antiforgery.xxxxxxx cookie?

解决方法

ASP.NET Core查找此cookie以查找X-CSRF令牌.

The ValidateAntiForgeryToken is an action filter that does Requests made to actions that have this filter applied will be blocked unless the request includes a valid antiforgery token.

通常,ASP.NET Core可能会在cookie或标头中查找令牌.所以你可能会遇到这种情况

>而不是cookie,标头用于传递令牌
>带有令牌的cookie具有与ASP.NET Core所期望的不同的名称.

认情况下,ASP.NET Core将生成并期望以DefaultCookiePrefix(“.AspNetCore.Antiforgery.”)开头的唯一cookie名称.

这可以使用防伪选项CookieName覆盖:

services.AddAntiforgery(options => options.CookieName = "X-CSRF-TOKEN-COOKIENAME");

对于.Net Core 2.0.0 or greater there will be changes

参考:
https://docs.microsoft.com/en-us/dotnet/api/Microsoft.AspNetCore.Antiforgery.AntiforgeryOptions?view=aspnetcore-2.0

对于以下用途:

services.AddAntiforgery(options => options.Cookie.Name = "X-CSRF-TOKEN-COOKIENAME");

如果谈论标题,可以通过以下方式指定名称

services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

调查:

> Preventing Cross-Site Request Forgery (XSRF/CSRF) Attacks in ASP.NET Core
> Antiforgery repo中的自述文件包含样本链接
> SO:Using the antiforgery cookie in ASP.NET Core but with a non-default CookieName

原文地址:https://www.jb51.cc/netcore/243383.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐