title: How do I pass the windows authentication token on a web request to an api?
body: <p>In ASP.NET Core 5, can I use Windows Authentication (which allows IIS to authenticate the user) and somehow grab that token and then use that to call a Web API application that also authenticates via Windows Authentication? In other words, we want the same security context in the API app as we have in the UI app. Is that even possible?</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddAuthentication(IISDefaults.AuthenticationScheme);
      var commonApiSettings = Configuration.GetSection(&quot;CommonApiSettings&quot;).Get&lt;CommonApiSettings&gt;(); 
      services.AddHttpClient(&quot;CommonApi&quot;,
                      client =&gt;
                      {
                          client.BaseAddress = new Uri(commonApiSettings.BaseAddress);
                          client.DefaultRequestHeaders.Add(&quot;Accept&quot;, &quot;application/json&quot;);
                          client.DefaultRequestHeaders.Add(&quot;User-Agent&quot;, &quot;AspNetCore-Demo&quot;);
                      });
      services.AddControllersWithViews();
  }
  </code></pre>
  <p>THen in my controller I want to call a web api. Every time I get 401 Unauthorized.</p>
  <pre><code>public class HomeController : Controller
  {
      private readonly ILogger&lt;HomeController&gt; _logger;
      private readonly IHttpClientFactory _clientFactory;
  
      public HomeController(ILogger&lt;HomeController&gt; logger, IHttpClientFactory httpClient)
      {
          _logger = logger;
          _clientFactory = httpClient;
      }
  
      public async Task&lt;IActionResult&gt; Index()
      {
  
          IEnumerable&lt;Confection&gt; inventory;
  
          try
          {
              var user = (WindowsIdentity)User.Identity!;
  
              await WindowsIdentity.RunImpersonatedAsync(user.AccessToken, async () =&gt;
              {
  
                  var impersonatedUser = WindowsIdentity.GetCurrent();
                  var message =
                      $&quot;User: {impersonatedUser.Name}\t&quot; +
                      $&quot;State: {impersonatedUser.ImpersonationLevel}&quot;;
  
                  var bytes = Encoding.UTF8.GetBytes(message);
            
                  var httpClient = _clientFactory.CreateClient(&quot;CommonApi&quot;);
                  httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Basic&quot;, impersonatedUser.AccessToken.ToString());
                  var request = new HttpRequestMessage(HttpMethod.Get, &quot;&quot;);
  
                  var httpResponseMessage = await httpClient.SendAsync(request);
  
                  if (httpResponseMessage.IsSuccessStatusCode)
                  {
                      using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync();
                      inventory = await JsonSerializer.DeserializeAsync&lt;IEnumerable&lt;Confection&gt;&gt;(contentStream);
                  }
              });
          }
          catch (Exception e)
          {
              //await context.Response.WriteAsync(e.ToString());
          }
  
          return View();
      }
  </code></pre>