title: Access to user credentials in ASP.NET Core middleware
body: <p>In ASP.NET core (2.1), running on a windows, I am using HttpSys configured with the following authentication schemes:</p>
  
  <pre><code>builder.UseHttpSys(options =&gt;
          {
              options.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
              options.Authentication.AllowAnonymous = true;
          })
  </code></pre>
  
  <p>Then in my Startup.Configure() method I am attempting to access the user credentials of the client calling the uri "/sensitiveOperation" as follows:</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          app.UseAuthentication();
  
          app.MapWhen(context =&gt; context.Request.Path.Equals("/sensitiveOperation") &amp;&amp; context.Request.Method.Equals(HttpMethods.Put), subApp =&gt;
          {                
              subApp.Run(async (context) =&gt;
              {
                  if (context.User.Identity.Name == "admin")
                  {
                      await context.Response.WriteAsync("Performing sensitive operation.");
                      // .. Do Sensitive operation....
                  }                    
              });
          });
  </code></pre>
  
  <p>The example is slightly vulgarised, but the main point is that context.User.Identity.Name is always empty where I would expect to see the name of the AD account which is making the call. Note that the call is done in powershell as follows:</p>
  
  <pre><code>Invoke-WebRequest -Uri http://localhost:5555/sensitiveOperation -Method Put -UseDefaultCredentials
  </code></pre>
  
  <p>I could put this code in a controller and use the [Authorize] attribute to get the credentials but I would prefer to do this operation before hitting the Mvc pipeline. Is there any way to get the user at this early stage of the pipeline ?</p>