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

Community
  • 1
  • 1
San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

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();
gilmishal
  • 1,884
  • 1
  • 22
  • 37