Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在asp.net中获取当前windows用户?_C#_Asp.net_Iis_Windows Authentication - Fatal编程技术网

C# 如何在asp.net中获取当前windows用户?

C# 如何在asp.net中获取当前windows用户?,c#,asp.net,iis,windows-authentication,C#,Asp.net,Iis,Windows Authentication,我尝试了很多代码,它在我的本地服务器上成功运行。但我试着把它放在远程服务器上,他们得到另一个字符串,比如IIS APPPOOL,“servername”。。。。我在IIS管理器中更改了一些设置,但失败。 如何在远程服务器中获取用户名 我尝试了一些代码,比如: System.Security.Principal.WindowsPrincipal= System.Threading.Thread.CurrentPrincipal as System.Security.Principal.Window

我尝试了很多代码,它在我的本地服务器上成功运行。但我试着把它放在远程服务器上,他们得到另一个字符串,比如IIS APPPOOL,“servername”。。。。我在IIS管理器中更改了一些设置,但失败。 如何在远程服务器中获取用户名

我尝试了一些代码,比如:

System.Security.Principal.WindowsPrincipal= System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal;

  string strName = p.Identity.Name;


HttpContext.Current.User
应该可以,但启用Windows身份验证还不够:还要确保禁用匿名身份验证。

假设您使用的是IIS 7.5,请执行以下操作。其他版本也将类似。所有上述获取用户名的方法都应该有效

1) Double click on Authentication (This can be done at server level and your web site level so check both!)
2) Right click on Anonymous authentication and disable it and likewise enable Windows authentication
3) Click Basic settings. Check which application pool your website is using. Ensure it is using pass-through authentication for an "Application User" by clicking "Connect As" i.e. A user using a browser will be the person requesting authentication.
4) Click "Application Pools". Click the Application pool from (3). Click "Advanced Settings". Check the Identity is set to ApplicationPoolIdentity


IIS上的身份验证设置是什么?
System.Security.Principal.WindowsIdentity.GetCurrent().Name
应该可以正常工作。请务必在IIS中提及您的身份验证设置。
HttpContext.Current.User
如果您具有正确的身份验证设置并且用户已登录,则应该可以使用。如果启用了匿名身份验证,则不会进行用户身份验证,而是使用应用程序池的帐户。如果使用任何其他类型的身份验证,
HttpContext.Current.User
将返回登录的用户。如果您同时拥有anonymous和其他功能,anonymous会覆盖其他选项。我尝试了这两种功能,但在本地服务器上都有效,但在远程服务器上不起作用。另外,我不知道在IIS管理器中要做什么。匿名身份验证也必须被禁用,否则它会优先-web浏览器总是先尝试匿名连接。非常感谢sarin,但我不明白为什么我会被设置为禁用匿名身份验证。它仍然不起作用。我无法获取Windows用户的用户名。您是否在服务器和网站上都禁用了匿名身份验证?我上面的图片只显示点击服务器,但你肯定需要点击网站并从那里禁用它。服务器是可选的,因为您可能希望您托管的其他网站以不同方式工作。当禁用匿名身份验证时,web站点希望获取,但我想输入用于身份验证的密码。如何设置服务器和网站?是的。有一个带有Mode=“Windows”的身份验证元素。如果它不起作用,最好的办法是构建一个小型测试应用程序,并将其部署以删除所有应用程序逻辑,并确保它与安装相关。试一试真正的问题是,如何在没有身份验证的情况下获取用户名?我不明白抱歉,沙林
string Name = Environment.GetEnvironmentVariable("USERNAME");
string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
    string usernamex = currentIdentity.Name.ToString();
HttpContext.Current.User.Identity.Name.ToString();
1) Double click on Authentication (This can be done at server level and your web site level so check both!)
2) Right click on Anonymous authentication and disable it and likewise enable Windows authentication
3) Click Basic settings. Check which application pool your website is using. Ensure it is using pass-through authentication for an "Application User" by clicking "Connect As" i.e. A user using a browser will be the person requesting authentication.
4) Click "Application Pools". Click the Application pool from (3). Click "Advanced Settings". Check the Identity is set to ApplicationPoolIdentity