4

我正在使用 asp.net 3.5 web.config 来限制访问,效果很好。

<authentication mode="Windows">
<authorization>
    <allow users="Bill, John"/>
    <deny users="*"/>
</authorization>

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

Server Error in '/' Application
Access is denied.
Description: An error occurred while .......
Error message 401.2: Unauthorized: Logon failed due to server configuration ...

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

<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="403" redirect="NoAccess.htm" />
    <error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>

但是,它只是行不通。我仍然收到系统错误消息,而不是我的自定义错误页面。

任何建议将不胜感激。

4

3 回答 3

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>

您需要确保应用程序池用户有权访问该路径。

于 2011-07-28T00:28:33.490 回答
4

没有在其他场景中对此进行过测试,但是查看了这篇详细文章中针对类似问题的一些建议。

另一个问题原来是:

授权要求阻止了对错误页面的访问。

解决方案是使用web.config 中的属性。请参阅链接以获取更详细的说明,但这里有一个片段:

<!-- in the same root web config file-->
<configuration>
    <system.web>
        <authorization>
        <allow users="Bill, John"/>
        <deny users="?" />
        </authorization>
    </system.web>

    <!-- the page specific authorization-->   
    <location path="GenericErrorPage.htm"> <!-- other ones for your other pages-->
        <system.web>
        <authorization>
        <allow users="*" />
        </authorization>
        </system.web>
    </location>

</configuration>
于 2011-07-28T00:19:09.273 回答
-1

改变 :

<customErrors mode="RemoteOnly" />

mode 属性可以是以下之一:

* On – error details are not shown to anybody, even local users. If you specified a custom error page it will be always used.
* Off – everyone will see error details, both local and remote users. If you specified a custom error page it will NOT be used.
* RemoteOnly – local users will see detailed error pages with a stack trace and compilation details, while remote users with be presented with a concise page notifying them that an error occurred. If a custom error page is available, it will be shown to the remote users only.

向访问者显示一个简洁但不那么漂亮的错误页面仍然不够好,因此您需要组合一个自定义错误页面并以这种方式指定它:

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/errors/GeneralError.aspx" 
/>
于 2011-03-18T06:11:15.530 回答