How to get the current Windows user in asp.net?

I have tried a lot of code that works successfully on my local server. But I tried to install the remote server to get other lines like IIS APPPOOL, "servername" .... I go to some settings in IIS manager but I was not successful. How can I get the username on the remote server

I have tried several codes like:

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

  string strName = p.Identity.Name;

      

or

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

      

or

string Name = Environment.GetEnvironmentVariable("USERNAME");

      

or

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

      

or

WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
    string usernamex = currentIdentity.Name.ToString();

      

or

HttpContext.Current.User.Identity.Name.ToString();

      

+3


source to share


2 answers


Assuming you are using IIS 7.5, follow these steps. Other versions will be similar. Then all of the above methods of getting the username will work.

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 usingCheck the Application pool settings

+4


source


HttpContext.Current.User

should be ok, but the fact that Windows Authentication is enabled is not enough: also make sure Anonymous Authentication is disabled.



+3


source







All Articles