title: .Net Core 2.1 getting client machines windows user name
body: <p>I am attempting to get the currently logged in user in Windows on the clients computer. I have tried using User.Identity.Name &amp; WindowsIdentity.GetCurrent().Name. When I run the application locally through Visual Studio, it runs great. Meanwhile when I publish this same code to our IIS server when I run the this code, it returns "NT AUTHORITY\NETWORK SERVICE".</p>
  
  <p>I thought this was going to be an easier task and started going through a bunch of different google searches with no luck (maybe its just that im new to .net core and am making a dumb mistake somewhere). Anyhow my server has Windows Authorization enabled (see image below).</p>
  
  <p><a href="https://i.stack.imgur.com/delQz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/delQz.png" alt="Windows Authentication IIS"></a></p>
  
  <p>The project is a .net core 2.1 web application and I am working in C#. Any help with this would be great!</p>
  
  <p><strong>Note</strong></p>
  
  <p>My previous thread (now deleted) for this question was marked as a duplicate question (<a href="https://stackoverflow.com/questions/36641338/how-get-current-user-in-asp-net-core">How get current user in asp.net core</a>) I do not understand how this is a duplicate when in this question they are saying that their HttpContext is null. I do not have this issue, I return a user but it is NOT the client machine's user. Due to this I had to recreate this question. If there is a duplicate answer please post it in the comments let me review if it actually is the same and if it is, then we can mark it as a duplicate. Thank you.</p>
  
  <p>I also tried using:</p>
  
  <pre><code>HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value
  </code></pre>
  
  <p>This was suggested in the duplicate answer post but this just gives me a null reference error.</p>
  
  <p><strong>Update #1</strong></p>
  
  <p>Just thought I would also include that my server has the following roles already installed:</p>
  
  <p><a href="https://i.stack.imgur.com/ckJ71.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ckJ71.png" alt="Windows Auth role installed"></a></p>
  
  <p><strong>Update #2</strong></p>
  
  <p>Here is the method getting the user name, it is the index.cshtml razor page that is initially loaded.</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; OnGetAsync()
      {
          ILogger log = ApplicationLogger.CreateLogger("LoginFilter");
          log.LogError("Start");
          log.LogError("contextIdent: " + User.Identity.Name);
          log.LogError("windowsIdent: " + WindowsIdentity.GetCurrent().Name);
          log.LogError("environment: " + Environment.UserName);
          log.LogError("End");
  
          return Page();
      }
  </code></pre>
  
  <p>This outputs the following to the stdout log:</p>
  
  <pre><code>Hosting environment: Production
  Content root path: C:\inetpub\wwwroot\MyApp
  Now listening on: http://127.0.0.1:20316
  Application started. Press Ctrl+C to shut down.
  fail: LoginFilter[0]
        Start
  fail: LoginFilter[0]
        contextIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        windowsIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        environment: MYENV$
  fail: LoginFilter[0]
        End
  </code></pre>
  
  <p><strong>Update #3</strong></p>
  
  <p>Added:</p>
  
  <p><code>log.LogError("findFirst: " + User.FindFirst(ClaimTypes.NameIdentifier).Value);</code></p>
  
  <p>After adding this the log generated the following:</p>
  
  <pre><code>Hosting environment: Production
  Content root path: C:\inetpub\wwwroot\MyApp
  Now listening on: http://127.0.0.1:35812
  Application started. Press Ctrl+C to shut down.
  fail: LoginFilter[0]
        Start
  fail: LoginFilter[0]
        contextIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        windowsIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        environment: MYENV$
  fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
        An unhandled exception has occurred while executing the request.
  System.NullReferenceException: Object reference not set to an instance of an object.
     at MyApp.Pages.IndexModel.OnGetAsync()
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject)
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object[] arguments)
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
     at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
     at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context)
  </code></pre>
  
  <p><strong>Update #4</strong></p>
  
  <p>I added to my Startup.cs ConfigureServices the following:</p>
  
  <pre><code>services.AddAuthentication(IISDefaults.AuthenticationScheme);
  </code></pre>
  
  <p>And into my index.cshtml:</p>
  
  <pre><code>var user = WindowsIdentity.GetCurrent();
  WindowsIdentity.RunImpersonated(user.AccessToken, () =&gt;
  {
      var impersonatedUser = WindowsIdentity.GetCurrent();
      log.LogError("impersonated: " + impersonatedUser.Name);
  });
  </code></pre>
  
  <p>As I had expected (due to the user being passed to impersonate still being "NT AUTHORITY\NETWORK SERVICE") the results are as follows:</p>
  
  <pre><code>Hosting environment: Production
  Content root path: C:\inetpub\wwwroot\MyApp
  Now listening on: http://127.0.0.1:36547
  Application started. Press Ctrl+C to shut down.
  fail: LoginFilter[0]
        Start
  fail: LoginFilter[0]
        impersonated: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        contextIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        windowsIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        environment: MYENV$
  fail: LoginFilter[0]
        hcIdent: NT AUTHORITY\NETWORK SERVICE
  fail: LoginFilter[0]
        End
  Application is shutting down...
  </code></pre>