How to get current Windows user name in ASP.NET Core 2.2, when web application runs as Administrator?

1.1k Views Asked by At

We have an ASP.NET Core 2.2 web-application, hosted on IIS and run under MyComputer\MyAdminUser Windows user (with administrator permissions). I need to get the name of current Windows user, which are working with our application, f.ex. MyComputer\MyClientUser. All client users are local users (MyComputer\ ...). How can I do it?

I've tried to use different approaches, but noone brings me expected result (MyClientUser):

1. HttpContext.Current.Request.LogonUserIdentity;

'HttpContext' does not contain a definition for 'Current' and no accessible extension method 'Current' accepting a first argument of type 'HttpContext' could be found...

2. Request.LogonUserIdentity;

the same problem with LogonUserIdentity

3. Request.ServerVariables["LOGON_USER"];

the same problem with ServerVariables

4. Request.Headers

This list doesn't include any information about current Windows user

5. Environment.UserName

The result is "MyAdminUser"

6. WindowsIdentity.GetCurrent().Name

The result is "MyComputer\MyAdminUser"

7. ManagementObjectCollection collection = 
      new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem")
      .Get();
   collection?.Cast<ManagementBaseObject>()?.First()?.Properties?["UserName"]?.Value

For Visual Studio result is correct (current Windows user), but on IIS the Properties?["UserName"]?.Value is null or empty (Properties?["UserName"]?.Name = "UserName")

What am I doing wrong and how can I get expected name?

0

There are 0 best solutions below