1

I have followed this link (Token Based Authentication in ASP.NET Core) to create the jwt token and I am successful to create the token

Below is the image

token creation

Now I want to validate the token I have passed the token as below

validation of token

I am getting 401 authorization.Please let me know the where I am doing wrong.

I have taken the same code as mention in the blog

Here is the code link

https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample

1 Answer 1

2

Your problem is this line in TokenController.cs:

 var handler = new JwtSecurityTokenHandler();

you can't just instantiate a new handler for every request. You need to use an handler created using your JwtBearerOptions - when you just instantiate you don't use the signingKey you placed in Startup.cs

public TokenController(IOptions<JwtBearerOptions> options)
{
    _bearerOptions = options.Value;
}

and in GetToken

JwtSecurityTokenHandler handler = _bearerOptions.SecurityTokenValidators.OfType<JwtSecurityTokenHandler>().First();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.