加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:23:21 所属栏目:百科 来源:网络整理
导读:我试图在.Net Core中使用ValidateAntiForgeryToken,但我得到了.AspNetCore.Antiforgery.xxxxxxx cookie丢失了. 这是什么.AspNetCore.Antiforgery.xxxxxxx cookie? 解决方法 ASP.NET Core查找此cookie以查找X-CSRF令牌. The ValidateAntiForgeryToken is an
我试图在.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读