2012-03-16 47 views
0

我们有一个内部系统,用户可以使用Windows身份验证进行身份验证,但我们希望在401 repsonse中包含一些自定义内容,将用户带到自定义用户名/密码身份验证页面如果取消掉Windows认证对话框。在ASP.NET中包含自定义内容Windows身份验证401响应

未认证用户访问一个网页,Windows身份验证一个401 Unauthorized响应来响应,与头

WWW-Authenticate: Negotiate 
WWW-Authenticate: NTLM 

这提示浏览器显示一个对话框,要求Windows凭据。如果用户输入他们的凭证,则会有另一个握手请求 - 响应,然后用户通过身份验证并显示请求的页面。

如果用户取消对话框,浏览器会显示原始401未授权响应的内容。 我们在Global.asax中使用Application_EndRequest来返回一些自定义内容,它将用户带到我们的自定义登录系统。

/// <summary> 
    /// If we are about to send the NTLM authenticate headers, include this content. Then, if the user cancels off the 
    /// windows auth dialog, they will be presented with this page and redirected to the username/password login screen 
    /// </summary> 
    void Application_EndRequest(object sender, EventArgs e) 
    { 
     Logger.DebugFormat("in Application_EndRequest. Response.StatusCode: {0}", Response.StatusCode); 

     if (Response.StatusCode == 401) 
     { 
      Response.ContentType = "text/html"; 

      var redirectUrl = string.Format("https://mycompany.com/SSO/Login?encryptedSessionRequestId={1}", 
       HttpUtility.UrlEncode(Request.QueryString["encryptedSessionRequestId"])); 

      var text = File.ReadAllText(Server.MapPath("~/UnauthorizedError.html")); 
      text = text.Replace("[USERNAME_PASSWORD_LOGON_REDIRECT]", redirectUrl); 


      Response.Write(text); 

      Logger.Debug("Done writing response"); 
      Response.End(); 
     } 
    } 

UnauthorizedError.html包含以下脚本,在转发

<script language="javascript" type="text/javascript"> 
    window.location = "[USERNAME_PASSWORD_LOGON_REDIRECT]"; 
</script> 

当(Win7的/ IIS7.5)本地运行,这个伟大工程的用户,我们使用的Response.Write()来发送我们的内容,用户可以在取消对话时看到它。

但是当远程访问该站点时,例如当它在我们的开发环境中运行时,尽管我们可以从日志中看到Application_EndRequest被调用,并且我们的内容写入响应,但它在某个时间点被覆盖,并且所有到达浏览器的是验证标头和文本You do not have permission to view this directory or page.

1)我们如何防止这些内容被覆盖?

2)在管道中哪里可能发生这种情况?

3)有没有人有任何其他方式来实现此行为的建议,例如与HTTP模块?

谢谢!

回答

0

我浪费了很多来解决这个问题,但没有直接的解决方案。 这是因为win身份验证适用于iis而非站点级别,您无法控制如何验证当前请求。 根据ip,在不同的登录页面上使用重定向有几种方法。

+1

我们重新设计了登录系统到底要避免这种情况,因为它似乎hackish的和不支持的。感谢您的输入。 – 2012-03-21 09:45:58

0

我知道这是一个旧帖子,但我想共享这个问题的解决方案。

当改变的远程请求(读:非本地主机)的响应消息,你需要将以下添加到您的配置文件:

<system.webServer> 
    <httpErrors existingResponse="PassThrough"></httpErrors> 
</system.webServer> 

如果不允许回应“穿过”远程客户端将获得默认"You do not have permission to view this directory or page"

我收到这个信息:https://stackoverflow.com/a/17324195/3310441