如何从ASP.NET页面获取当前登录的Windows帐户?

我有一个使用ASP.NET表单身份validation的ASP.NET 3.5应用程序。 我希望能够在页面中编辑数据时获取当前登录到计算机的Windows用户名(不登录到ASP.NET应用程序,但是登录到Windows)。

如果我使用Context.User.Identity.Name.Tostring() ,我会获得登录ASP.NET应用程序的用户名,但我需要Windows帐户名。

 System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() 

此外,它仅在我从Visual Studio运行网站时有效,但在部署到IIS后,它返回NT AUTHORITY \ SYSTEM

您必须在配置中将身份validation模式设置为Windows ,并禁用授权标记中的匿名用户。

要将当前登录的用户转换为Windows帐户,您必须使用Windows authentication而不是Forms authentication

System.Security.Principal.WindowsIdentity.GetCurrent()。Name.Tostring()也仅在我从visual studio运行网站时起作用,但在部署到IIS后它返回NT AUTHORITY \ SYSTEM

它显示了应用程序当前用户。 在Visual Studio Web服务器上托管应用程序时,它使用您的本地帐户。 但是,当您使用不同凭据登录Web应用程序时,它将始终显示您当前的Windows登录名。

部署到IIS的应用程序在您的情况下使用NT AUTHORITY \ SYSTEM帐户。

要使用C#将当前登录的用户导入Windows,请使用:

 string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); 

我努力奋斗,奋力拼搏。 其中一件事是我无法访问被锁定的IIS,因此我无法更改任何服务器设置。 我不得不在代码中使用我能做的事情。 当我研究它时,许多回复说,“像这样建立IIS”。 。 .well,当你有权访问IIS时,这很棒,但我没有 – 我必须使用我在代码中可以做的事情。 所以,我最终像这样处理它:

在我的Web配置文件中,我在该部分中添加了以下代码行:

         

然后,它在我的本地返回了一个错误,我必须进入并修复。 我去了我的机器上以下路径中的applicationhost.config文件(你的可能不同):

C:\ users \“您的用户名”\ My Documents \“yourIISInstallation”\ config \ applicationhost.config

我将以下设置更改为“允许”,已将其设置为“拒绝”:

 

变成

 

 

 

在里面

  

部分。 在我发现这个问题之前,我正在把头发拉出来。 我希望这可以帮助别人。 一旦我将上面的代码放入webconfig文件中,它就在Intranet上运行,它只是在我的本地返回错误,但是一旦我将上面的内容添加到我的本地applicationhost.config文件中,它就开始在我的本地工作了同样。 然后,我调用以下变量来返回Windows上登录用户的名称:

  HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\\")) + 1); 

干杯!

我用这个:

 System.Security.Principal.WindowsPrincipal user; user = new WindowsPrincipal(this.Request.LogonUserIdentity); this.Request.LogonUserIdentity.Impersonate(); user_name = user_name.Substring(user_name.LastIndexOf("\\") + 1); 
 string strName = HttpContext.Current.User.Identity.Name.ToString(); 

就像你想要它做的那样是正确的,但是你需要首先设置web服务器,参考如何使用ASP.NET获取Window NT Logged用户名 (设置Web服务器的第一步)。

尝试使用以下代码行:

 string loggedOnUser = string.Empty; loggedOnUser = Request.ServerVariables.Get("AUTH_USER"); 

从Visual Studio运行应用程序时可能无法获取值…在IIS中部署后检查它。

要获取用户名,请使用:

 string userName = string.Empty; using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "Your Domain Name")) { UserPrincipal user = new UserPrincipal(pc); user = UserPrincipal.FindByIdentity(pc, "User ID Will Come here"); if (user != null) { userName = user.GivenName + " " + user.Surname; } else { //return string.Empty; userName = "User Not Found"; } } 

我设法通过以下链接中的方法1中的说明解决此问题 – https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when- you-browse-a-web-site-that-uses-integrate简而言之,禁用除Windows身份validation之外的所有身份validation方法。 在管理员帐户下打开注册表,找到HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Lsa \ MSV1_0,右键单击该节点并转到“新建”,然后选择“多字符串值”。 输入“BackConnectionHostNames”并单击Enter。 对于“值”,输入您尝试设置访问权限的网站,然后单击“确定”。 重启IIS一旦我完成了我能够使用HttpContext.Current.User.Identity.Name获取当前的Windows用户,WindowsPrincipal(this.Request.LogonUserIdentity)也让我登录了Windows用户名。参考System.Environment .UserName和System.Security.Principal.WindowsIdentity.GetCurrent()。Name,这两个仍然给了IIS用户。

这让我花了很长时间才到达底线。 祝它好运。 IIS是一个醒来的噩梦!