By Damien Guard
This document will help you troubleshoot your configuration if you get 401 (Unauthorized) response from your API.We recommend that you log in to follow this quickstart with examples configured for your account. If the configuration of your JSON Web Token (JWT) middleware does not match the JWT that was passed to the API, you get a 401 (Unauthorized) response from your API. This document will help you troubleshoot your JWT middleware configuration.Check the Token Validation
There are 5 criteria for validating a JWT token.- Is the token formed properly? Check if the structure of the token matches the structure of a JSON Web Token. Read more about the JSON Web Token structure.
- Has the token been tampered with? The last part of a JWT is the signature. The signature is used to verify that the token was signed by the sender and not altered in any way.
- 
Has the token been received in its validity period?
JWTs are only valid for a specified time, defined in the expclaim.
- 
Is the token coming from the intended Authority?
Check the following two criteria:
- Signature verification: Check if the JWT is correctly signed with the key issued by the issuing authority.
- Issuer value: The Issuer is defined in the issclaim. Check if this claim matches up with what your application expects.
 
- 
Is the token intended for the current application?
Check if the audclaim of the JWT matches with what your application expects.
Inspect a Token
You can inspect a JWT with the JWT.io website. Use the debugger on the website to check if your JWT is well formed. You can also inspect values of the various claims. The screenshot below shows the following information:- The token is signed with the RS256 algorithm
- The issuer of the token is https://jerrie.auth0.com/
- The audience of the token is https://rs256.test.api

- The token is signed with the HS256 algorithm
- The issuer of the token is https://jerrie.auth0.com/
- The audience of the token is https://hs256.test.api

Debug Configuration Issues Using Log Files
To debug potential configuration issues, inspect the log files for your application. For more information, refer to the Logging in ASP.NET Core document. In this example, we run the application from the command line and inspect the console log output.1. Are you passing the JWT in the Authorization header?
Check if you are passing the JWT as a Bearer token in theAuthorization header of the HTTP request.
If you are not passing the token, you will see the following warning:

Authorization header of the HTTP request.
2. Did you configure the JWT middleware for the correct signing algorithm?
Make sure that the signing algorithm you used to sign your token matches the signing algorithm configured in your middleware. The following screenshots show two messages:- A warning message: “Authorization failed…”
- A message with more information


3. Has your token expired?
Each JSON Web Token is valid until the time defined in theexp claim runs out. If you send an expired token, the token will be rejected:

exp claim is a numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time. If you want to see the date/time for the value, visit EpochConverter.
4. Did you configure the correct issuer?
The Issuer specified in your token must match exactly with your JWT middleware configuration.
ValidIssuer property of TokenValidationParameters.
For RS256 tokens, the JWT middleware downloads the OIDC discovery document from Authority and configures the Issuer based on the issuer attribute specified in that document.
If you are using RS256 tokens, the system checks their signature before it checks the Issuer.
5. Does the audience match your JWT middleware configuration?
Check if the audience specified in your token matches your JWT middleware configuration.
- Set the correct Audienceproperty ofJwtBearerOptions
- Set the ValidAudienceproperty ofTokenValidationParameters