繁体   English   中英

如何在C#Web APP中获取当前的Windows登录名?

[英]How to get current Windows Login Name in C# Web APP?

如何获取Windows登录名? 我使用了以下方法:

  • Environment.UserName
  • WindowsIdentity.GetCurrent()。Name;

但是,当我将其放在IIS服务器中时,它将使用服务器的名称,而不是我的计算机的名称。

尝试使用Page.User.Identity.Name 这应该是您要寻找的。 此属性从HttpContext派生,表示当前HTTP请求的已登录用户安全信息。

如果结果为空,那么我会怀疑IIS设置没有正确配置。 尝试以下链接中的建议:

http://forums.asp.net/t/1689878.aspx/1
HttpContext.Current.User.Identity.Name为空

使用此System.Environment.GetEnvironmentVariable(“ UserName”)

看一眼:

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

以下是上面链接中的主要示例:

方案1:禁用模拟的IIS中的匿名身份验证。

HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
HttpContext.Current.Request.IsAuthenticated -> False
HttpContext.Current.User.Identity.Name  -> –
System.Environment.UserName -> ASPNET
Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\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    -> COMPUTER1\ASPNET

方案3:IIS中的匿名身份验证,在

HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
HttpContext.Current.Request.IsAuthenticated -> False
HttpContext.Current.User.Identity.Name  -> –
System.Environment.UserName -> IUSR_COMPUTER1
Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\IUSR_COMPUTER1

方案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: Identity of the running process on server.
SERVER1\IUSR_SERVER1: Anonymous guest user defined in IIS.
MYDOMAIN\USER1: The user of the remote client.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM