2014-11-06 124 views
1

我正在开发一个使用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没有改变?

回答

1

步骤1:打开Web.config文件并进行以下修改:

<!— 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="2880" /> 
</authentication> 
--> 

<authentication mode="Windows" /> 

步骤2:默认情况下MVC应用程序使用窗体身份验证和简单的会员,所以你需要让“假'为了运行Windows身份验证。

<appSettings> 
    <add key="webpages:Version" value="2.0.0.0" /> 
    <add key="webpages:Enabled" value="false" /> 
    <add key="PreserveLoginUrl" value="true" /> 
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 

    <add key="autoFormsAuthentication" value="false" /> 
    <add key="enableSimpleMembership" value="false"/> 

</appSettings> 

步骤3:在解决方案资源管理,然后在属性浏览器中选择项目名称,单击要启用Windows身份验证。

第4步:在属性资源管理器中,如果您希望完整的网站用于开发服务器上的经过身份验证的用户,您可以禁用匿名身份验证。

Reference

+0

谢谢你,你是对的,这一切都是必需的。但我已经正确设置了所有这些设置... – Landeeyo 2014-11-06 11:33:38

相关问题