Home > Net >  How to get current logged in user of Domain of local system in mvc
How to get current logged in user of Domain of local system in mvc

Time:01-01

I have an application for an organization where a user will logged in to his system with domain user, so now I want to get that logged in user. I tried many methods and get username locally, but my application is on IIS and it gets the IIS user with IIS APPPool\DefaultAppPool while system name and domain get but username not.

This is the code I am using for getting username:

Request.ServerVariables.Get("logon_user");
System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Environment.GetEnvironmentVariable("USERNAME");
System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).Identity.Name;

If someone knows how to get local username rather than IIS, please share. Thanks in advance.

CodePudding user response:

If you have Windows Authentication working, then the current user is available in HttpContext.User.

User.Identity.Name will be the username.

  • Related