title: 502.3 when calling AcquireTokenAsync in .NET Core 2.2
body: <p>I'm encountering a problem. I am using Microsoft Graph to get the current logged in user via <code>OnBehalfOfMsGraphAuthenticationProvider.cs</code> as seen in the following <a href="https://github.com/juunas11/azure-ad-on-behalf-of-sample-aspnetcore" rel="nofollow noreferrer">solution</a>.</p>
  
  <p>This has been working flawlessly, but I have been doing some refactoring, and suddenly I get an error when trying to execute my <code>authContext.AcquireTokenAsync()</code> method.</p>
  
  <pre><code>HTTP Error 502.3 - Bad Gateway
  </code></pre>
  
  <p>The code in question looks like this:</p>
  
  <pre><code>public async Task AuthenticateRequestAsync(HttpRequestMessage request) {
      var httpContext = _httpContextAccessor.HttpContext;
  
      //Get the access token used to call this API
      string token = await httpContext.GetTokenAsync("access_token");
  
      //We are passing an *assertion* to Azure AD about the current user
      //Here we specify that assertion's type, that is a JWT Bearer token
      string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
  
      //User name is needed here only for ADAL, it is not passed to AAD
      //ADAL uses it to find a token in the cache if available
      var user = httpContext.User;
      string userName =
          user.FindFirst(ClaimTypes.Upn).Value ?? user.FindFirst(ClaimTypes.Email).Value;
  
      var userAssertion = new UserAssertion(token, assertionType, userName);
  
      //Construct the token cache
      var cache = new DistributedTokenCache(user, _distributedCache,
          _loggerFactory, _dataProtectionProvider);
  
      var authContext = new AuthenticationContext(_configuration["AzureAd:Instance"] +
          _configuration["AzureAd:TenantId"], true, cache);
  
      var clientCredential = new ClientCredential(_configuration["AzureAd:ClientId"],
          (string) _configuration["AzureAd:ClientSecret"]);
  
      //Acquire access token
      var result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential, userAssertion); //This is where it crashes
  
      //Set the authentication header
      request.Headers.Authorization = new AuthenticationHeaderValue(result.AccessTokenType, result.AccessToken);
  }
  </code></pre>
  
  <p>I am calling it from my <code>OrdersController</code>:</p>
  
  <pre><code>// POST: api/Orders
  [HttpPost]
  public async Task&lt;IActionResult&gt; CreateAsync([FromBody] OrderDTO order) {
          if (!ModelState.IsValid) {
              return BadRequest(ModelState);
          }
  
          var graphUser = await this.graphApiService.GetUserProfileAsync();
  </code></pre>
  
  <p>The refactoring has consisted of dividing my solution into two class library projects and one web project - the latter has the controllers and the React app. <code>GraphAPiClient</code> and the provider are located in the Core library like this:</p>
  
  <p><a href="https://i.stack.imgur.com/YscBG.png" rel="nofollow noreferrer">Screenshot of architecture</a></p>