title: Get system logged in username in c# that also works in IIS
body: <p>I thought its simple to get the logged in username, in fact, it works fine with most of the responses available on stack overflow but when I publish the website all method fails.
  Can anybody please guide me to get the name of the logged in user from Windows with following condition.</p>
  
  <ol>
  <li><p>I just need logged in user name so forget about AD when you write the response.</p></li>
  <li><p>I cannot change to Windows authentication mode because user may be a guest user who is not part of company but still have access to my intranet website</p></li>
  <li>it’s not possible to change the user browser setting as there are more than 6000 users</li>
  <li><p>I cannot disable the anonymous authentication as I want everyone to be able to use the website</p></li>
  <li><p>I have already tried following solution and all works fine when I run the website on debug mode but all fails to return the username when I publish the website on IIS so please help me with some new as solution</p></li>
  </ol>
  
  <p></p>
  
  <pre><code>string _windowLogonUserName = System.Environment.UserName.ToString()
  </code></pre>
  
  <p></p>
  
  <pre><code>string _windowLogonUserName = 
      WindowsIdentity.GetCurrent().Name.Remove(0, _adDomainName.Length + 1)
  </code></pre>
  
  <p></p>
  
  <pre><code>string _windowLogonUserName = 
      System.Web.HttpContext.Current.User.Identity.Name.ToString(); 
  </code></pre>
  
  <p></p>
  
  <pre><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  </code></pre>
  
  <p></p>
  
  <pre><code>AppDomain appDomain = Thread.GetDomain();
  appDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
  WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
  Logger.Write("username principal" + windowsPrincipal.Identity.Name);
  Request.ServerVariables["LOGON_USER"]
  </code></pre>