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
Nguồn
2014-12-12 16:13:55
không này đánh bại pur sử dụng xác thực Windows? – cortijon
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