Home » Development » Ways to get Active Directory logon name in ASP.NET page

Ways to get Active Directory logon name in ASP.NET page

I’ve recently spent some time to find a best way to get visitors’ Active Directory name in my ASP.NET page. You’ll find a list of techniques.

Note: Some of them may require cookies to be enabled or need Anonymous Access to be disabled in IIS. Also you may need to do string parsing in some cases.

Request.ServerVariables["HTTP_COOKIE"];
Request.ServerVariables["LOGON_USER"]
Request.ServerVariables["REMOTE_USER"];
Request.ServerVariables["UNMAPPED_REMOTE_USER"];
Request.ServerVariables["AUTH_USER"];
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
HttpContext.Current.User.Identity.Name;
HttpContext.Current.Request.LogonUserIdentity.Name
System.Security.Claims.ClaimsPrincipal.Current.Identity.Name
httpContextAccessor.HttpContext.Current
System.Threading.Thread.CurrentThread
System.Threading.Thread.CurrentPrincipal

Note: If you use Windows Authentication and ASP.NET Identity together, one of them may overwrite the user data in the objects above. As a result, you may not be able to find out the Windows authenticated user or logged in user. In this case, try switching on and off useCookiePolicy to find out the both user names.

Ned Sahin

Blogger for 20 years. Former Microsoft Engineer. Author of six books. I love creating helpful content and sharing with the world. Reach me out for any questions or feedback.

Leave a Comment