如何在 Windows 身份验证中使用自定义错误页面
我正在使用 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>
然而,它就是行不通。我仍然收到系统错误消息,而不是我的自定义错误页面。
任何建议将不胜感激。
I am using asp.net 3.5 web.config to limit access and it works great.
<authentication mode="Windows">
<authorization>
<allow users="Bill, John"/>
<deny users="*"/>
</authorization>
Unauthorized (but authenticated) users will be blocked by a system error message saying that:
Server Error in '/' Application
Access is denied.
Description: An error occurred while .......
Error message 401.2: Unauthorized: Logon failed due to server configuration ...
In order to make the message more friendly, I uncomment the customErrors flag and create a GenericErrorPage.htm in the root path of my project.
<customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
However, it just doesn't work. I still get the system error message rather than my custom error page.
Any suggestions will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不会看到它 - 自定义错误页面由 ASP.NET 应用程序提供,但 Windows 身份验证由 IIS 本身提供。
现在您可以将 IIS 设置为使用不同的错误页面。对于 IIS7,这需要一个单独的配置部分;
您需要确保应用程序池用户有权访问该路径。
You won't see it - custom error pages are served by the ASP.NET application, but Windows auth is served up by IIS itself.
Now you can set IIS to use different error pages. For IIS7 this needs a separate configuration section;
And you'll need to ensure the app pool user has access to that path.
没有在其他场景中对此进行测试,但查看了此 中的一些建议类似问题的详细文章。
另一个问题是:
解决方案是在 web.config 中使用 属性。请参阅链接以获取更详细的解释,但这里有一个片段:
Not having tested this in other scenarios, but looking at some of the suggestions from this detailed article for a similar problem.
The other problem turned out to be:
The solution was to use a attribute in the web.config. refer to the link for more detailed explanation but here's a snippet:
change :
mode 属性可以是以下之一:
向访问者显示简洁但不太漂亮的错误页面仍然不够好,因此您需要组合一个自定义错误页面并以这种方式指定它:
change :
The mode attribute can be one of the following:
Displaying a concise yet not-so-pretty error page to visitors is still not good enough, so you need to put together a custom error page and specify it this way: