我正在開發一個使用Windows身份驗證的MVC Web應用程序。其目的是在頁面打開時允許自動日誌記錄,但允許按需求按不同用戶進行簽名。我試圖使用這裏的代碼'Login as another user' MVC 4 Windows Authentication和這裏http://www.roelvanlisdonk.nl/?p=825但他們都沒有爲我工作。以不同的用戶身份登錄ASP.MVC不會改變用戶身份
我已經簡化的情況下,以最大的,所以它看起來如下:
public string Logout()
{
AuthenticationAttempts = AuthenticationAttempts + 1;
if (AuthenticationAttempts == 1)
{
this.Send401();
}
var domain = User.Identity.Name.Split('\\')[0];
var user = User.Identity.Name.Split('\\')[1];
return string.Format("Domain: {0}<br>User: {1}", domain, user);
}
/// <summary>
/// Send a 401 response
/// </summary>
public void Send401()
{
// Create a 401 response, the browser will show the log-in dialogbox, asking the user to supply new credentials,
// if browser is not set to "automaticaly sign in with current credentials"
Response.Buffer = true;
Response.StatusCode = 401;
Response.StatusDescription = "Unauthorized";
// A authentication header must be supplied. This header can be changed to Negotiate when using keberos authentication
Response.AddHeader("WWW-Authenticate", "NTLM");
// Send the 401 response
Response.End();
}
private int _authenticationAttempts = 0;
public int AuthenticationAttempts
{
get
{
if (!string.IsNullOrEmpty(string.Format("{0}", Session["AuthenticationAttempts"])))
{
int.TryParse(Session["AuthenticationAttempts"].ToString(), out _authenticationAttempts);
}
return _authenticationAttempts;
}
set
{
_authenticationAttempts = value;
Session["AuthenticationAttempts"] = _authenticationAttempts;
}
}
當我打電話註銷操作方法,第一次我得到的窗口標誌,但是當我點擊好的,User.Identity仍然如此。
編輯:
我發現
Request.ServerVariables["LOGON_USER"]
新登錄用戶身份存儲,但爲什麼User.Identity沒有改變?
謝謝你,你是對的,這一切都是必需的。但我已經正確設置了所有這些設置... – Landeeyo 2014-11-06 11:33:38