如何在asp.net中使用Windows身份验证获取用户名?

时间:2013-10-30 07:31:57

标签: c# asp.net authorization windows-authentication windows-security

我想使用Windows身份验证获取用户名

实际上我实现了“以不同用户身份登录”,单击此按钮时,Windows安全性将出现在那里,我们可以提供凭据。

在那个时候,如果我提供一些其他凭证,它只接受当前用户名。 如何从Windows安全性获取给定的凭据用户名?

在IIS中托管应用程序,然后禁用匿名身份验证并启用Windows身份验证。

的web.config:

<system.web>
    <compilation debug="true" targetFramework="4.0" />
  <identity impersonate="true"/>
  <authorization>
      <allow users="*"/>
      <deny users="*"/>
  </authorization>
</system.web>
<system.webServer>
    <directoryBrowse enabled="true" />
    <security>
        <authentication>
            <anonymousAuthentication enabled="false" />
            <windowsAuthentication enabled="true" />
        </authentication>
    </security>

的.cs

这里我的默认用户名始终是

string fullName = Request.ServerVariables["LOGON_USER"];

有什么想法吗?提前致谢

8 个答案:

答案 0 :(得分:28)

您可以通过以下方式获取Windows身份验证下的用户WindowsIdentity对象:

WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

然后您可以获得有关用户的信息,例如identity.Name。

请注意,您需要为这些代码安装HttpContext。

答案 1 :(得分:12)

这些是您可以访问的不同变量及其值,具体取决于IIS配置。

方案1: IIS中的匿名身份验证已关闭模拟功能。

HttpContext.Current.Request.LogonUserIdentity.Name    SERVER1\IUSR_SERVER1 
HttpContext.Current.Request.IsAuthenticated           False                    
HttpContext.Current.User.Identity.Name                –                        
System.Environment.UserName                           ASPNET                   
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\ASPNET

方案2: IIS中的Windows身份验证,模拟关闭。

HttpContext.Current.Request.LogonUserIdentity.Name    MYDOMAIN\USER1   
HttpContext.Current.Request.IsAuthenticated           True             
HttpContext.Current.User.Identity.Name                MYDOMAIN\USER1   
System.Environment.UserName                           ASPNET           
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\ASPNET

方案3::IIS中的匿名身份验证,模拟处于

HttpContext.Current.Request.LogonUserIdentity.Name    SERVER1\IUSR_SERVER1 
HttpContext.Current.Request.IsAuthenticated           False                    
HttpContext.Current.User.Identity.Name                –                        
System.Environment.UserName                           IUSR_SERVER1           
Security.Principal.WindowsIdentity.GetCurrent().Name  SERVER1\IUSR_SERVER1 

方案4::IIS中的Windows身份验证,模拟已启用

HttpContext.Current.Request.LogonUserIdentity.Name    MYDOMAIN\USER1
HttpContext.Current.Request.IsAuthenticated           True          
HttpContext.Current.User.Identity.Name                MYDOMAIN\USER1
System.Environment.UserName                           USER1         
Security.Principal.WindowsIdentity.GetCurrent().Name  MYDOMAIN\USER1

传奇
SERVER1\ASPNET:服务器上正在运行的进程的标识。
SERVER1\IUSR_SERVER1:在IIS中定义的匿名来宾用户。
MYDOMAIN\USER1:远程客户端的用户。

Source

答案 2 :(得分:4)

这应该有效:

User.Identity.Name

Identity会返回IPrincipal

以下是Microsoft documentation的链接。

答案 3 :(得分:2)

这取决于应用程序的配置以及IIS,因为下面链接中的这位绅士已正确解释。请参阅下面的文章

http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/

答案 4 :(得分:0)

我认为由于以下代码,您没有获得新凭据

string fullName = Request.ServerVariables["LOGON_USER"];

您可以尝试自定义登录页面

答案 5 :(得分:0)

您获得的用户名是这样的:

var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;

答案 6 :(得分:0)

您可以从Name阅读WindowsIdentity

var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return Ok(user);

答案 7 :(得分:-1)

在您的视图页面/_Layout.cshtml 中声明以下代码:

@using System.DirectoryServices.AccountManagement

@{
    var context = new PrincipalContext(ContextType.Domain);
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
    var userName = @principal.GivenName + " " + @principal.Surname;
}