I'm developing an MVC web application using the Windows authentication. The aim is to allow automated logging when the page is opened but allow signing as different user on demand. I'm trying to use the code from here 'Login as another user' MVC 4 Windows Authentication and here http://www.roelvanlisdonk.nl/?p=825 but none of them is working for me.
I've simplified the case to the maximum, so it looks as follows:
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;
}
}
When I call Logout action method for the first time I'm getting the sign in window, but when I click okay the User.Identity is still as it was.
EDIT:
I found that
Request.ServerVariables["LOGON_USER"]
stores newly logged user identity, but why User.Identity isn't changing?