如何在Windows身份验证中使用自定义错误页面

non*_*aku 4 .net asp.net web-config custom-error-pages

我使用asp.net 3.5 web.config来限制访问,它工作得很好.

<authentication mode="Windows">
<authorization>
    <allow users="Bill, John"/>
    <deny users="*"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)

未经授权(但已通过身份验证)的用户将被系统错误消息阻止,说明:

Server Error in '/' Application
Access is denied.
Description: An error occurred while .......
Error message 401.2: Unauthorized: Logon failed due to server configuration ...
Run Code Online (Sandbox Code Playgroud)

为了使消息更友好,我取消注释customErrors标志并在项目的根路径中创建GenericErrorPage.htm.

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="403" redirect="NoAccess.htm" />
    <error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)

但是,它只是不起作用.我仍然收到系统错误消息而不是我的自定义错误页面.

任何建议将不胜感激.

blo*_*art 10

您将看不到它 - 自定义错误页面由ASP.NET应用程序提供,但Windows身份验证由IIS本身提供.

现在您可以将IIS设置为使用不同的错误页面.对于IIS7,这需要一个单独的配置部分 ;

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Auto">
    <error statusCode="403" 
           subStatusCode="-1" 
           prefixLanguageFilePath="" 
           path="C:\inetpub\wwwroot\errors\403.htm" 
           responseMode="File" />
  </httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

您需要确保应用程序池用户可以访问该路径.