ASP.NET中的401.2的customerrors

我在ASP.NET中成功实现了基于角色的授权.当一个人没有所需的角色时,他会看到401.2未经授权的错误页面.

我现在想要完成的是在我的应用程序中有一个自定义的401页面,并通过web.config中的设置将其重定向到那里.我试过这个:

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
    <error statusCode="401" redirect="NoAccess.htm" />
</customErrors>

但这并没有被抓住.我是否必须在IIS中覆盖它?我希望不会因为这会使事情更难部署.

解决方法

我最近遇到了同样的问题,事实证明这是使用Windows身份验证时的一个怪癖.

Joshua Flanagan不久前创建了一个nice HttpModule,它将尊重web.config中的customErrors部分并重定向到401错误页面.

The key to the solution is to intercept the EndRequest event of the page lifecycle,check for a 401 status code,and then execute your custom page.

HttpModule的可移植性很好,因为它使解决方案可以重用,并保持你的Global.asax干净,但是如果你真的想要的话,没有什么可以阻止你用他的代码在Global.asax中连接你的EndRequest事件.

如果您使用的是ASP.NET MVC,那么解决方案就不那么优雅了.

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...