【问题标题】:Global Error Handling on AutoValidateAntiforgeryToken failureAutoValidateAntiforgeryToken 失败的全局错误处理
【发布时间】:2022-01-24 03:27:22
【问题描述】:

我有这个 ASP.Net 核心网络应用程序。我在Startup.cs 中配置了一些自定义错误处理程序。在这里你可以看到:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), app =>
    {
        app.UseStatusCodePagesWithReExecute("/api/error-{0}");
    });

    app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"), app =>
    {
        app.UseStatusCodePagesWithRedirects("/error-{0}");
    });

    //...
}

然后有一个控制器,上面有[AutoValidateAntiforgeryToken]

问题是当AntiForgeryToken 验证失败时,请求得到响应,状态码为 400,并且我的自定义错误处理程序不起作用。如果我发送了正确的 AntiForgeryToken 或者只是删除了 [AutoValidateAntiforgeryToken] 属性,那么自定义错误处理程序就可以正常工作。

有没有办法让[AutoValidateAntiforgeryToken]失败结果使用全局错误处理程序?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core error-handling antiforgerytoken


    【解决方案1】:

    #SOLVED 出现问题的原因是自定义错误处理程序本身的操作在同一个控制器内;因此,错误处理程序操作方法本身需要防伪令牌!通过在错误处理程序操作方法上方添加[IgnoreAntiForgeryToken] 解决。

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 2023-04-01
      • 2010-11-25
      • 1970-01-01
      • 2013-03-06
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多