2010-05-04 155 views
13

我通过使用集成Windows身份验证并关闭匿名访问来限制对网站的访问。这样我就可以向他们展示他们的真实姓名(从查找Active Directory并使用服务器变量LOGON_USER)并执行其他相关的Active Directory任务。使用集成Windows身份验证时以不同用户身份登录

我该如何再次通过“以其他用户身份登录”链接提示他们的用户凭据,显示浏览器提示(例如,您将在Chrome或Firefox等浏览器上获得的,或者该网站不在IE中的“Intranet”区域)而不是Web表单?

由于SharePoint提供了此功能,我假设有一种方法可以通过代码实现此目的,但我不知道哪些代码可以执行此操作(使用C#)。我可以发送一个401头使提示出现,但是如何确认他们是否已登录?

+0

这难道不是打败PUR使用Windows身份验证的姿势? – cortijon 2010-05-04 15:46:00

+6

不,因为您可能想要执行管理任务而不必注销Windows。 Windows身份验证仍在使用,但我想在用户之间切换,而无需注销或在浏览器可执行文件上执行“运行”。由于SharePoint具有此功能,因此提供它有一定的价值。 – SamWM 2010-05-04 15:56:42

回答

1

试试这种方法。它是基于方法Microsoft.SharePoint.ApplicationPages.AccessDeniedPage.LogInAsAnotherUser()

首先,我用javascript由于SharePoint做类似的东西访问AccessDeniedPage页的反汇编代码:

function GoToSignAs() { 
    window.location.replace("./SignAs.aspx?signAs=true&returnUrl=" + window.location.toString()); 
} 

<a onclick="GoToSignAs(); return false;" href="javascript:;">SignAs</a> 

然后,在你的页面AccessDeniedPage您使用此:

public partial class SignAs : Page 
{ 
    private const string LoginAttempts = "LoginAttempts"; 

    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 
     HttpContext current = HttpContext.Current; 
     if (current == null) 
     { 
      throw new InvalidOperationException(); 
     } 
     if (GetUrlParameter<bool>("signAs")) 
     { 
      HandleSignAs(current, GetUrlParameter<string>("returnUrl")); 
     } 
    } 

    // ... 

    private static void HandleSignAs(HttpContext context, string returnUrl) 
    { 
     int attempts = 0; 
     HttpCookie attemptsCookie = context.Request.Cookies[LoginAttempts]; 
     if (attemptsCookie == null || string.IsNullOrEmpty(attemptsCookie.Value)) 
     { 
      attemptsCookie = new HttpCookie(LoginAttempts); 
     } 
     else 
     { 
      attempts = int.Parse(attemptsCookie.Value, CultureInfo.InvariantCulture); 
     } 

     if (!string.IsNullOrEmpty(context.Request.Headers["Authorization"])) 
     { 
      // Attempts are counted only if an authorization token is informed. 
      attempts++; 
     } 

     if (attempts>1) 
     { 
      attemptsCookie.Value = string.Empty; 
      context.Response.Cookies.Add(attemptsCookie); 
      context.Response.Redirect(returnUrl, true); 
     } 
     else 
     { 
      attemptsCookie.Value = attempts.ToString(CultureInfo.InvariantCulture); 
      context.Response.Cookies.Add(attemptsCookie); 
      SendEndResponse(context, 401, "401 Unauthorized"); 
     } 
    } 

    private static void SendEndResponse(HttpContext context, int code, string description) 
    { 
     HttpResponse response = context.Response; 
     context.Items["ResponseEnded"] = true; 
     context.ClearError(); 

     response.StatusCode = code; 
     response.Clear(); 
     response.StatusDescription = description; 

     response.AppendHeader("Connection", "close"); 
     response.AddHeader("WWW-Authenticate", "Negotiate"); 
     response.AddHeader("WWW-Authenticate", "NTLM"); 

     response.End(); 
    } 
} 

FIX:您必须使用IIS才能正常工作

相关问题