如何在asp.net中获取当前的Windows用户?

时间:2015-05-06 07:22:51

标签: c# asp.net iis windows-authentication

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

我尝试了一些代码:

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

  string strName = p.Identity.Name;

string strName = HttpContext.Current.User.Identity.Name.ToString();

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();

2 个答案:

答案 0 :(得分:4)

假设您使用的是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

Double click on Authentication

Right click on Anonymous authentication and disable it and likewise enable Windows authentication

Check which application pool your website is using Check the Application pool settings

答案 1 :(得分:3)

HttpContext.Current.User应该没问题,但 Windows身份验证已启用的事实还不够:还要确保禁用匿名身份验证。