1

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?

Vitaliy
  • 645
  • 1
  • 10
  • 21
  • 1
    You'll need to implement [Windows Authentication](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-2.2&tabs=visual-studio) in your ASP.NET Core application. – Knelis Jun 05 '19 at 13:06
  • @Knelis is it possible to combine Windows Authentication with Individual User Accounts, which have been already implemented in this web-app (AspNetUsers, AspNetRoles and so on)? – Vitaliy Jun 05 '19 at 13:14
  • @Knelis I need just current Windows user's name, nothing more. Everything else works with Individual User Accounts – Vitaliy Jun 05 '19 at 13:51
  • None of the above should be used. For ASP.NET Core, use `ControllerBase.User` https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.user?view=aspnetcore-2.2 – Lex Li Jun 05 '19 at 13:57

0 Answers0