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

本文关键字:身份验证 Windows 获取 用户 asp net | 更新日期: 2023-09-27 18:37:21

我想使用 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"];

有什么想法吗?

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

这些是您有权访问的不同变量及其值,具体取决于 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:远程客户端的用户。

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

WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

然后,您可以获取有关用户的信息,例如身份。名字。

请注意,这些代码需要有 HttpContext。

这应该有效:

User.Identity.Name

Identity返回一个IPrincipal

这是指向Microsoft文档的链接。

这取决于应用程序的配置以及 IIS,正如下面链接中的这位先生正确解释的那样。请看他下面的文章

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

您可以从WindowsIdentity中读取Name

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

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

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

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

用户名,

如下所示:

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

在视图页面/_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;
}