I want to get user name using Windows authentication
Actually I implemented \"Sign in as different user\",when click this button Windows security will appear there w
This should work:
User.Identity.Name
Identity
returns an IPrincipal
Here is the link to the Microsoft documentation.
It depends on the configuration of the application as well as on IIS as this gentleman in the below link has rightfully explained. Please see his article below
http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/
You can read the Name
from WindowsIdentity
:
var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return Ok(user);
I think because of the below code you are not getting new credential
string fullName = Request.ServerVariables["LOGON_USER"];
You can try custom login page.
Username you get like this:
var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;
You can get the user's WindowsIdentity object under Windows Authentication by:
WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;
and then you can get the information about the user like identity.Name.
Please note you need to have HttpContext for these code.