user1934825
user1934825

Reputation: 21

Get system logged in username in c# that also works in IIS

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.

  1. I just need logged in user name so forget about AD when you write the response.

  2. 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

  3. it’s not possible to change the user browser setting as there are more than 6000 users
  4. I cannot disable the anonymous authentication as I want everyone to be able to use the website

  5. 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

string _windowLogonUserName = System.Environment.UserName.ToString()

string _windowLogonUserName = 
    WindowsIdentity.GetCurrent().Name.Remove(0, _adDomainName.Length + 1)

string _windowLogonUserName = 
    System.Web.HttpContext.Current.User.Identity.Name.ToString(); 

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

AppDomain appDomain = Thread.GetDomain();
appDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
Logger.Write("username principal" + windowsPrincipal.Identity.Name);
Request.ServerVariables["LOGON_USER"]

Upvotes: 2

Views: 4298

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

It sounds like you're publishing the site outside of the AD domain. if the server is not on the domain, it won't be able to authenticate (or even accept) Windows users. The basic answer is that you can't get it.

If the server is internal (on the domain) then you can enabled integrated windows auth in IIS. Here's a reference.

Upvotes: 2

Related Questions