如何禁用剃刀页中的CSRF抗试验

问题描述 投票:0回答:2
我想在测试服务器下运行时禁用CSRF检查,因此运行自动测试时不必阅读并发送令牌。

为“有用的魔术”蔓延到ASP.NET核心,我被卡住了。

模板代码中没有任何内容显然添加了此内容,但在此期间查看调试器中的过滤器

services.AddMvc(options => options.Filters)

呼叫中没有显示全局过滤器。

该代码也不起作用。

mvcOptions.Filters.Add<IgnoreAntiforgeryTokenAttribute>(0);

没有禁用选项。

我可以做到吗?

trone this:

Antiforgery.Options

您也可以在PageModel上忽略它:

services.AddMvc().AddRazorPagesOptions(o => { o.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); });

asp.net-core
2个回答
9
投票

所接受的解决方案只会忽略CSRF检查,但它仍然会以Cookie的形式发布CSRF令牌。
要改变这一点,您需要注册自定义反手术服务:

[IgnoreAntiforgeryToken(Order = 1001)] public class IndexModel : PageModel
有一些实现:

// Replace the default anti-forgery service with your custom implementation // Last Registration Wins: The DI container uses the last registered implementation of a service, so order matters. // Always register your custom IAntiforgery implementation after calling AddRazorPages() to ensure it overrides the default registration builder.Services.AddSingleton<Microsoft.AspNetCore.Antiforgery.IAntiforgery, DisableAntiforgery>();


0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.