6

如何获取 Windows 登录的用户名?我使用了这些方法:

  • 环境.用户名
  • WindowsIdentity.GetCurrent().Name;

但是,当我将它放入 IIS 服务器时,它会使用服务器的名称,而不是我的机器。

4

3 回答 3

6

试试Page.User.Identity.Name。这应该是您正在寻找的。此属性派生自HttpContext并表示当前 HTTP 请求的登录用户安全信息。

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

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

于 2012-05-15T20:37:04.893 回答
0

使用这个 System.Environment.GetEnvironmentVariable("UserName")

于 2013-06-20T23:03:22.083 回答
0

看一眼:

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.
于 2019-06-03T14:53:51.473 回答