title: Unauthorized response with Invalid Audience error for Azure AD + ASP.Net Core 2.1
body: <p>I have developed an UI and Web API using ASP.net Core 2.1 with Azure AD authentication. Both are registered with Azure App registration. I am using below code in UI. But I am getting an Unauthorized error.</p>
  <pre><code>string AZURE_AD_INSTANE = &quot;https://login.microsoftonline.com/&quot;;
  string TENANT_ID = &quot;&lt;tenant GUID&gt;&quot;;
  string CLIENT_ID = &quot;&lt;Client GUID ofWeb API&gt;&quot;;
  string SECRET = &quot;&lt;Secret created for Web API under Certificates &amp; secrets&gt;&quot;;
  string RESOURCE = &quot;https://MyOrg.onmicrosoft.com/TestWebAPI&quot;; //Application ID URI set in Expose an API
  ClientCredential ClientCredential = new ClientCredential(CLIENT_ID, SECRET);
  string authority = String.Format(&quot;{0}{1}&quot;, AZURE_AD_INSTANE, TENANT_ID);
  
  
  AuthenticationContext authContext = new AuthenticationContext(authority);
  string accessToken = authContext.AcquireTokenAsync(RESOURCE, ClientCredential).Result.AccessToken;
  
  HttpClient client = new HttpClient();
  client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, accessToken);
  HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, &quot;https://localhost:44326/api/values/Get&quot;);
  request.Headers.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;, accessToken);
  HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult();
  
  string status = response.StatusCode.ToString();
  </code></pre>
  <p>StartUp.cs</p>
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
        services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
                  .AddAzureADBearer(options =&gt; Configuration.Bind(&quot;AzureAd&quot;, options)); 
         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/fGd1K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fGd1K.png" alt="enter image description here" /></a></p>
  <p>I am getting a access token. When I check in jwt.io, it says 'Signature Verified'. But the API call gives unauthorized response status code. When I check the response header, it has the information as &quot;{Bearer error=&quot;invalid_token&quot;, error_description=&quot;The audience is invalid&quot;}&quot;</p>
  <p>How can I resolve this?</p>