Forbid() 返回 404

Ian*_*ton 6 asp.net-web-api asp.net-core-2.0

我已从 Web 请求返回Forbid(),浏览器声称收到 404 而不是 403。

我已经向 cookie 身份验证添加了一个处理程序,如下所示......

o.Events.OnRedirectToAccessDenied += ctx =>
{
   ctx.Response.StatusCode = (int)HttpStatusCode.Forbidden;

   return Task.FromResult(0);
};
Run Code Online (Sandbox Code Playgroud)

但这似乎没有被调用。

那么,为什么应该返回 403 的方法却返回 404 呢?

Han*_*fer 1

我遇到过同样的问题。这些是我修复它的步骤:

  1. 暂时删除所有异常处理,例如app.UseExceptionHandler("/Home/Error")from Startup. 就我而言,404发生这种情况是因为异常处理被破坏。
  2. 然后我得到了“真正的”错误:我的问题是我没有配置身份验证,因此没有身份验证方案。

在我添加正确的身份验证选项后Startup(在我的例子中,我使用了自定义身份验证处理程序),我得到了正确的响应代码:

services.AddAuthentication(LicenseKeyAuthenticationOptions.Scheme)
        .AddScheme<LicenseKeyAuthenticationOptions, LicenseKeyAuthenticationHandler>(LicenseKeyAuthenticationOptions.Scheme, null);
Run Code Online (Sandbox Code Playgroud)