为“有用的魔术”蔓延到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());
});
所接受的解决方案只会忽略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>();