title: How to debug JWT and AuthorizeAttribute
body: <p>I am following <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/azure-ad-b2c-webapi?view=aspnetcore-2.2" rel="noreferrer">this Microsoft Docs tutorial</a> and everything appears to work upto the point where I call the web api with the access token using Postman.</p>
  
  <p>To be specific:</p>
  
  <ul>
  <li>I request a token, Azure AD redirects me to supply Username and Password</li>
  <li>Credentials are accepted and a token is returned</li>
  <li>I tell Postman to use the token and call the api</li>
  <li>I get a 401</li>
  </ul>
  
  <p>If I decode the token everything appears correct, i.e. the <code>aud</code> matches the Web API registration in AD B2C, User is correct, claims, etc.</p>
  
  <p>Commenting out the <code>[Authorize]</code> attribute in my API works, data is returned so its something in the authorization of my API I think.</p>
  
  <p>I'm not sure how I can debug the authentication process in ASP.NET Core Web API though. For example putting breakpoints in my controller doesn't work because my code doesn't even get that far.</p>
  
  <h3>UPDATE</h3>
  
  <p>In the Postman Console the Response Headers say: <code>WWW-Authenticate:"Bearer error="invalid_token", error_description="The issuer is invalid""</code>. The issuer is <code>https://login.microsoftonline.com/&lt;guid&gt;/v2.0/</code>.</p>
  
  <h3>UPDATE 2</h3>
  
  <p>Embarrassingly, as documented in the article, once I matched the <code>AzureAdB2C.Instance</code> setting in my <code>appsettings.json</code> with the Postman <code>Auth Url</code> the <code>invalid_token</code> error goes away but now I'm getting 500 errors: <code>Unable to obtain configuration from..., Microsoft.IdentityModel.Protocols.ConfigurationManager&amp;lt;T&amp;gt;.GetConfigurationAsync(CancellationToken cancel)</code>.</p>
  
  <h3>BRICK WALL</h3>
  
  <pre><code>The thread 0x5c30 has exited with code 0 (0x0).
  Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET https://localhost:44366/api/values  
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Net.Sockets.dll'. 
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\Microsoft.Win32.Primitives.dll'. 
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Net.NameResolution.dll'. 
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Security.Cryptography.Encoding.dll'. 
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Collections.NonGeneric.dll'. 
  Exception thrown: 'System.Net.Http.HttpRequestException' in System.Net.Http.dll
  Exception thrown: 'System.IO.IOException' in Microsoft.IdentityModel.Protocols.dll
  Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
  Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
  Exception thrown: 'System.InvalidOperationException' in Microsoft.IdentityModel.Protocols.dll
  Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Diagnostics.StackTrace.dll'. 
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.2.5\System.Reflection.Metadata.dll'. 
  Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Error: Exception occurred while processing message.
  System.InvalidOperationException: IDX20803: Unable to obtain configuration from: '[PII is hidden]'. ---&gt; System.IO.IOException: IDX20804: Unable to retrieve document from: '[PII is hidden]'. ---&gt; System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
     at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
     at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
     --- End of inner exception stack trace ---
     at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
     at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel)
     at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
     --- End of inner exception stack trace ---
     at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
     at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
     at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
     at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
     at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
     at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
     at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
     at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
     at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
  'iisexpress.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\2.2.5\Microsoft.AspNetCore.Http.Extensions.dll'. 
  Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1438.8301ms 500 text/html; charset=utf-8
  </code></pre>