惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
宝玉的分享
宝玉的分享
V
Visual Studio Blog
B
Blog RSS Feed
G
Google Developers Blog
U
Unit 42
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Palo Alto Networks Blog
月光博客
月光博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
小众软件
小众软件
GbyAI
GbyAI
L
LangChain Blog
A
About on SuperTechFans
Attack and Defense Labs
Attack and Defense Labs
T
Tor Project blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
The Register - Security
The Register - Security
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
P
Privacy International News Feed
I
Intezer
W
WeLiveSecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Help Net Security
Help Net Security
NISL@THU
NISL@THU
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Vulnerabilities – Threatpost
N
News | PayPal Newsroom
Security Archives - TechRepublic
Security Archives - TechRepublic
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security

博客园 - 灵魂边缘

Detecting the File Download Dialog In the Browser The IE 'non-disappearing content' bug 10个强大的Javascript表单验证插件推荐 使用javascript检测客户端是否是mobile的类型,如果是就跳转到另外一个页面 在javascript中对一个对象数组进行排序 Devenv /ResetSkipPkgs Invalid Viewstate error and how to fix it? 在asp.net中如何管理cache 用Javascript取得URL参数(Get URL parameters/query string using Javascript) - 灵魂边缘 用JavaScript对表格排序 使用 jQuery 简化 Ajax 开发 Localization/Globalization in ASP.Net 2.0 Generating change scripts in SQL Server 2005 Management Studio .net中完成海量数据的批量插入和更新 - 灵魂边缘 - 博客园 存储过程跨系统跨数据库操作 ASP.NET 2.0 中配合Master Page 使用CSS - 灵魂边缘 正则表达式全部符号解释 - 灵魂边缘 - 博客园 [转]asp.net导出数据到Excel的三种方法 ASP.NET:创建Linked ValidationSummary, 深入理解ASP.NET的Validation (转)
Redirecting to custom 401 page when "Access denied" occures within an ASP.NET application - 灵魂边缘
灵魂边缘 · 2009-10-12 · via 博客园 - 灵魂边缘

Introduction

If you have an ASP.NET application with authentication mode set to Windows:

 <authentication mode="Windows"/> 
 <authorization>
         <deny users="?" />
 </authorization>

Then all Windows users can access your pages but those without Windows login (from internet) will receive an ASP.NET "Access Denied 401" page. I have decided to replace this default message with some custom page. After couple of hours Googling, I found out that this is a very common problem and all the workarounds which seem to be reasonable does not work. Here are some of them:

  1. Use a custom error page in Web.config:
    <customErrors defaultRedirect="ErrorPage.asp&shy;x" mode="On"> 
        <error statusCode="401" redirect="AccessDenied.aspx" />       
    </customErrors>

    This works fine with statusCode="404" but not with 401.

  2. Try to catch an unauthorized request in one of the application events in Global.asax.cs:
    protected void Application_AuthenticateRequest(Object sender, 
                                                        EventArgs e)
    {
     if (!Request.IsAuthenticated) Response.Redirect(
                                                "AccessDenied.aspx"); 
    }

    It is also useless, because even the browsers with valid credentials make two posts, one without credentials and having Request.IsAuthenticated=false and the other one with credentials and having Request.IsAuthenticated=true. Browsers without credentials make only one post. It means you are not able to determine if the first post having Request.IsAuthenticated=false comes from an unauthorized browser or not.

The problem

Then I found an answer from a Guru at microsoft.public.dotnet.framework.aspnet.security newsgroup.

That's correct. There's no way around that. The way wininet authentication works is that if the resource you are requesting does not allow anonymous access, a 401 is sent back to the browser. If the resource is using Windows Integrated authentication and the browser is configured to automatically send credentials, the token is sent back and the user is authenticated. In the case of Basic authentication, a login prompt is displayed and the user must log in.

If you intercept the 401 and redirect somewhere, you hijack the browser's ability to challenge. There is no way around that.

Jxx Cxxxxxx, MCSE, MCSD [MSFT], Developer Support, ASP.NET.

Resolution

That was also the result of my research. The whole thing works like this:

  1. The browser sends request without credentials to the server.
  2. Server rejects this request and answers with "401 Access Denied".
  3. Browser recognizes 401 and if it has appropriate credentials does not show this message. The second request with credentials will be posted. The requested page will be sent to the browser.
  4. If the browser has no credentials the second post will not take place. The received "401 Access Denied" will be shown.

The workaround is to manipulate the content of "401 Access Denied" response. The browser uses the header of this response to determine 401 case. It means we can manipulate the HTML content without influencing the whole challenge. For instance we can add the following code in the Application_EndRequest event of Global.asax.cs.

  protected void Application_EndRequest(Object sender, 
                                             EventArgs e)
  { 
     HttpContext context = HttpContext.Current;
     if (context.Response.Status.Substring(0,3).Equals("401"))
     {
        context.Response.ClearContent();
        context.Response.Write("<script language="javascript">" + 
                     "self.location='../login.aspx';</script>");
     } 
  }

Now, the whole thing works like this:

  1. The browser sends request without credentials to the server.
  2. Server rejects this request and answers with "401 Access Denied" Header + our JavaScript.
  3. Browser recognizes 401 and if it has appropriate credentials does not show this message. Our HTML will not be rendered and JavaScript will not be executed. The second request with credentials will be posted. The requested page will be sent to the browser.
  4. If the browser has no credentials, the second post will not take place. The received "401 Access Denied" will be shown. Our HTML will be rendered and JavaScript will be executed. The client side redirection will take place. The browser will show a custom 401 page.