2010-05-04 37 views
13

Tôi đã hạn chế quyền truy cập vào trang web bằng cách sử dụng Xác thực Windows Tích hợp và tắt quyền truy cập ẩn danh. Bằng cách này, tôi có thể hiển thị tên thật của họ (từ tìm kiếm trên Active Directory và sử dụng biến máy chủ LOGON_USER) và thực hiện các tác vụ Active Directory khác có liên quan.Đăng nhập với tư cách người dùng khác khi sử dụng Xác thực Windows Tích hợp

Làm cách nào để tôi có thể nhắc lại thông tin đăng nhập của người dùng, thông qua liên kết 'đăng nhập với người dùng khác', hiển thị lời nhắc của trình duyệt (như bạn sẽ truy cập trình duyệt như Chrome hoặc Firefox hoặc nếu trang web không có trong vùng 'Intranet' trong IE) thay vì một Biểu mẫu Web?

Vì SharePoint cung cấp chức năng này, tôi giả sử có cách để thực hiện điều này thông qua mã, nhưng tôi không biết mã nào có thể thực hiện việc này (sử dụng C#). Tôi có thể gửi tiêu đề 401 làm cho lời nhắc xuất hiện, nhưng làm cách nào để bạn xác nhận xem họ có đăng nhập không?

+0

không này đánh bại pur sử dụng xác thực Windows? – cortijon

+6

Không, bởi vì bạn có thể muốn thực hiện các tác vụ quản trị mà không phải đăng xuất khỏi Windows. Xác thực Windows vẫn được sử dụng, nhưng tôi muốn chuyển đổi giữa những người dùng mà không phải đăng xuất hoặc thực hiện 'chạy dưới dạng' trên trình duyệt thực thi. Vì SharePoint có chức năng này, có một số giá trị trong việc cung cấp nó. – SamWM

Trả lời

1
+0

Dường như không làm việc cho tôi. Khi tôi đăng nhập, LogonUserIdentity vẫn giống như trước đây. Trong thực tế, nó thậm chí không xác thực - bất kỳ tên người dùng và mật khẩu nhập vào kết thúc lên chuyển hướng trở lại trang chủ. – SamWM

1

Hãy thử phương pháp này. Nó được dựa trên mã tháo rời của Microsoft.SharePoint.ApplicationPages.AccessDeniedPage.LogInAsAnotherUser() phương pháp

Trước hết, tôi truy cập vào trang AccessDeniedPage sử dụng javascript vì Sharepoint làm điều gì đó tương tự:

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

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

sau đó, trong trang của bạn AccessDeniedPage bạn sử dụng này:

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: bạn phải sử dụng IIS để làm việc đúng cách

Các vấn đề liên quan