我正在开发一个使用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