【问题标题】:Override global AutoValidateAntiforgeryTokenAttribute on one Action在一个操作上覆盖全局 AutoValidateAntiforgeryTokenAttribute
【发布时间】:2019-05-08 03:06:24
【问题描述】:

我正在使用 Startup.cs 中配置的全局 AutoValidateAntiforgeryTokenAttribute,但我需要为两个操作禁用它,有没有办法覆盖它,或者这样做时是全部还是没有?当整个应用程序中只有两个不需要它的Action时,我宁愿不将Attribute添加到每个Action中......

public IServiceProvider ConfigureServices(IServiceCollection services)
{
  services.AddMvc(options => {
    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()); 
  });
}

【问题讨论】:

    标签: asp.net-mvc asp.net-core antiforgerytoken


    【解决方案1】:

    在文档中搜索了一段时间后,我找到了答案……action方法可以用IgnoreAntiforgeryToken Attribute修饰。

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.ignoreantiforgerytokenattribute?view=aspnetcore-2.2

    [HttpPost]
    [AllowAnonymous]
    [IgnoreAntiforgeryToken]
    public async Task<IActionResult> MyAction()
    { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 2013-05-27
      • 2019-07-14
      • 1970-01-01
      • 2013-07-10
      • 2016-11-07
      • 2020-07-28
      • 1970-01-01
      相关资源
      最近更新 更多