title: How to get the access token in a service in .net core?
body: <p>I have an application and I want to use a hybrid flow to call an API from my MVC web application. The API is secured and requires an access token (JWT). The code part on the API side is done.
  But how can I send my access token from my MVC application to the API in a service? I want to use a "generic" service for this that uses an httpclient.</p>
  
  <p>I know that in controllers you can use</p>
  
  <pre><code>var accessToken = await HttpContext.GetTokenAsync("access_token");  
  </code></pre>
  
  <p>or</p>
  
  <pre><code>var accessToken = await HttpContext.GetUserAccessTokenAsync();
  </code></pre>
  
  <p>But how can I access the access token in a service? 
  Tried this too, but didn't work</p>
  
  <pre><code>    var accessToken = _httpContextAccessor.HttpContext.Request.Headers[HeaderNames.Authorization];
  </code></pre>
  
  <p>Side question: how can I set the access token for the httpclient in the constructor of my service? (Using async methods is not possible) so I can't use this:</p>
  
  <pre><code>protected HttpClient HttpClient { get; private set; }
  
  public MyGenericHttpClientService(IHttpContextAccessor httpContextAccessor)
  
  {
              var accessToken = await httpContextAccessor.HttpContext.GetTokenAsync("access_token");
              HttpClient.SetBearerToken(accessToken);
  }
  </code></pre>