更新时间:2023-12-04 09:56:34
我一直在寻找相同的问题,并且似乎没有Windows身份验证事件,即使Authenticate事件对于表单和Windows也很常见.
I was looking at the same issue, and looks like there is no events for windows authentication, even that Authenticate event is common for forms and windows.
但是我找到了解决方案!
But I found a solution to this!
http://www.codeproject.com/Articles/11202/Redirecting-to-custom-401-page-when-quot-Access-de
更新
摘自原始文章
protected void Application_EndRequest(Object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context.Response.Status.Substring(0,3).Equals("401"))
{
if(User.Identity.IsAuthenticated)
{
// this means user is authenticated, but 401 still returned
// which means no access to page or whatever you are trying to access?
}
}
}
UPDATE2
我还发现该解决方案并非在所有情况下都有效.我在不同的环境中进行测试,因此为我工作,但为其他人工作.
I also found out that this solution doesn't work in all cases. I was testing in different environments, so was working for me, but not for others.
默认情况下,IIS甚至不会运行这段代码,只是返回它自己的错误页面,您要做的就是告诉IIS让应用程序处理错误.
By default IIS will not even run this piece of code and just return it's own error page, what you want to do is tell IIS to let app handle errors.
<httpErrors existingResponse="PassThrough">
</httpErrors>
现在,这样说来,IIS将不再返回任何自定义错误,您将需要在应用程序中处理它们,即.不仅是401,还有403、405等
Now, with that said, IIS won't return any more of custom errors and you will need to handle them in application, ie. not only 401, but 403, 405, etc