简体   繁体   English

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

[英]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: 我使用了以下方法:

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

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.

相关问题 如何在C#中获取Windows登录名 - how to get windows login name in c# 如何在C#中获取当前应用程序的名称 - How to get the name of current application in C# 使用 C# 在 Windows 10 中获取当前活动的应用程序名称 - Get current active application name in Windows 10 using C# 使用C#将登录名获取到另一个Windows表单中 - Get login name in to another windows form in C# 如何使用运行我的 Windows App Store 应用程序的 C# 获取当前目录? - How do I get the current directory using C# that my Windows App Store app is running in? 如何使用C#获取Sharepoint中当前Web服务器的名称或IP - How to get the name or IP of current web server in Sharepoint using C# 如何在Web API C#后端登录时获取用户电子邮件和名称? - How do I get user email, name upon login in Web API C# backend? 如何在C#.net Web应用程序中无论当前区域性如何获取特定的资源字符串 - How to get specific resource string regardless of current culture in c# .net web app 我如何从 web api 获取数据到 ac# windows 窗体应用程序类 - How do i get data from a web api into a c# windows forms app class 如何在Windows 10通用应用程序上的C#中获取本地主机名 - How to get local host name in C# on a Windows 10 universal app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM