title: Automatically Attaching Access Token to HTTP Client in Blazor wasm
body: <p>I am using an open id connect identity provider for my Blazor wasm application and would like an access token to be attached to the http client as described in <a href="https://docs.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-3.1#attach-tokens-to-outgoing-requests" rel="nofollow noreferrer">this</a> article.</p>
  <p>However, whenever I create the http client and try to use it I am getting an AccessTokenNotAvailableException, even when logged in.</p>
  <p>Here is what I have:</p>
  <p>In Program.cs</p>
  <pre class="lang-cs prettyprint-override"><code>// Add service for CustomAuthorizationMessageHandler
  builder.Services.AddScoped&lt;CustomAuthorizationMessageHandler&gt;();
  
  // Http client for requests to API
  builder.Services.AddHttpClient(&quot;API&quot;, client =&gt;
  {
      // Set base address to API url
      client.BaseAddress = new Uri(&quot;https://localhost:44370&quot;);
  
      // Set request headers to application/json
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  }).AddHttpMessageHandler&lt;CustomAuthorizationMessageHandler&gt;();
  
  // Auth0 authentication setup
  builder.Services.AddOidcAuthentication(options =&gt;
  {
      builder.Configuration.Bind(&quot;Auth0&quot;, options.ProviderOptions);
      options.ProviderOptions.ResponseType = &quot;code&quot;;
  });
  </code></pre>
  <p>CustomAuthorizationHandler.cs</p>
  <pre class="lang-cs prettyprint-override"><code>public class CustomAuthorizationMessageHandler : AuthorizationMessageHandler
  {
      public CustomAuthorizationMessageHandler(IAccessTokenProvider provider,
          NavigationManager navigationManager)
          : base(provider, navigationManager)
      {
          ConfigureHandler(
              authorizedUrls: new[] { &quot;https://localhost:44370&quot; },
              scopes: new[] { &quot;admin&quot; });
      }
  }
  </code></pre>
  <p>When trying to access the api</p>
  <pre class="lang-cs prettyprint-override"><code>try
  {
      var client = ClientFactory.CreateClient(&quot;API&quot;);
      message = await client.GetStringAsync(&quot;api/message&quot;);
  }
  catch (AccessTokenNotAvailableException e)
  {
      message = &quot;You CANNOT access the api. Please log in&quot;;
  }
  </code></pre>
  <p>Currently, the below code works to get the message from the api when removing the AddHttpMessageHandler call in Program.cs but I don't want to have to get the access token every time I make an api call. TokenProvider is of type IAccessTokenProvider and is injected in.</p>
  <pre class="lang-cs prettyprint-override"><code>var client = ClientFactory.CreateClient(&quot;API&quot;);
  using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, &quot;api/message&quot;))
  {
      var tokenResult = await TokenProvider.RequestAccessToken();
  
      if (tokenResult.TryGetToken(out var token))
      {
          requestMessage.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, token.Value);
          var response = await client.SendAsync(requestMessage);
          if(response.IsSuccessStatusCode)
          {
              message = await response.Content.ReadFromJsonAsync&lt;string&gt;();
          }
          else
          {
              message = await response.Content.ReadAsStringAsync();
          }
      }
      else
      {
          message = &quot;You CANNOT access the api. Please log in&quot;;
      }
  }
  </code></pre>
  <p>How can I fix this so I don't get an AccessTokenNotAvailableException every time?</p>