customerrors for 401.2 in ASP.NET

后端 未结 4 842
梦谈多话
梦谈多话 2020-12-19 03:57

I successfully implemented role based authorization in ASP.NET. When a person does not have the needed role he gets to see an error page for 401.2 not authorized.

W

4条回答
  •  眼角桃花
    2020-12-19 04:34

    I ran into the same problem recently and it turns out that this is one of the quirks when using Windows Authentication.

    Joshua Flanagan created a nice HttpModule a while ago that will respect the customErrors section in your web.config and redirect to the 401 error page.

    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.

    The portability of the HttpModule is nice because it makes the solution reusable, and keeps your Global.asax clean, but there's nothing stopping you from wiring up your EndRequest event in the Global.asax with his code if you really wanted to.

    If you're using ASP.NET MVC, the solution isn't quite as elegant.

提交回复
热议问题