[英]How to get current Windows Login Name in C# Web APP?
How can I get the user name of Windows Login? 如何获取Windows登录名? I used these methods:
我使用了以下方法:
But, when I PUT this inside IIS server, it takes the name of the server, not my machine. 但是,当我将其放在IIS服务器中时,它将使用服务器的名称,而不是我的计算机的名称。
Try Page.User.Identity.Name
. 尝试使用
Page.User.Identity.Name
。 This should be what you're looking for. 这应该是您要寻找的。 This property is derived from
HttpContext
and represents the logged in user security information for the current HTTP request. 此属性从
HttpContext
派生,表示当前HTTP请求的已登录用户安全信息。
If the result is null, then I would suspect that the IIS settings is not configured properly. 如果结果为空,那么我会怀疑IIS设置没有正确配置。 Try the advice in the following links:
尝试以下链接中的建议:
http://forums.asp.net/t/1689878.aspx/1 http://forums.asp.net/t/1689878.aspx/1
HttpContext.Current.User.Identity.Name is Empty HttpContext.Current.User.Identity.Name为空
使用此System.Environment.GetEnvironmentVariable(“ UserName”)
Take a look at: 看一眼:
https://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/ https://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/
Here are the main examples from the link above: 以下是上面链接中的主要示例:
Scenario 1: Anonymous Authentication in IIS with impersonation off. 方案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
Scenario 2: Windows Authentication in IIS, impersonation off. 方案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
Scenario 3: Anonymous Authentication in IIS, impersonation on 方案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
Scenario 4: Windows Authentication in IIS, impersonation on 方案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
Note: 注意:
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.