具有 Windows 身份验证的自定义状态代码页

Custom status code page with Windows Authentication

我在 ASP NET Core 3.1 中使用 Windows 身份验证进行了项目。我想使用自定义错误代码页。 这是一个基本示例。

在家庭控制器中:

 [Route("/Error/401")]
    public IActionResult StatusCode401()
    {
        HttpContext.Response.StatusCode = 401;
        return View("ErrorUnauthorized");
    }

但是当我调用 StatusCode401 方法时,Windows 出现提示对话框(登录名、密码),如果我取消它,我会看到我的自定义视图。 如果我禁用 Windows 身份验证,它工作正常。

如何在我使用 Windows 身份验证时禁用 Windows 提示对话框?

谢谢

您可以打开 web.config,

然后找到statusCode 401,将responseModelExecuteURL更改为文件

<system.webServer>
    <httpErrors errorMode="Custom" >
        <error statusCode="403" subStatusCode="0" prefixLanguageFilePath="" path="***" responseMode="ExecuteURL" />
        <error statusCode="401" subStatusCode="2" prefixLanguageFilePath="" path="***" responseMode="File" />
        ....
    </httpErrors>
</system.webServer>