0

I am new to JSON Web Key Sets. I would like to automatic download JWKS for JWT token validation.

I did google but not find any example using .net core webapi 6.O

itmannz
  • 529
  • 9
  • 19

1 Answers1

0

I learned from this thread that we need to download the .well-known file every time we validate the jwt token. I excerpted the content of the answer and put it below to prevent the link from being invalid and inaccessible.

Please do cache the result of the .well-known file. It will change, though extremely rarely. Your code must handle when the key used to sign the token does not appear in your cache, then you will have to re-download the file.

John

We can download the jwks file when verifying the jwt token. In .net core, we can use HttpClient. And we can download jwks via the url like below

https://your_domain/.well-known/jwks.json

The most important thing is we need to use Microsoft's Nuget packages Microsoft.IdentityModel.Tokens and System.IdentityModel.Tokens.Jwt.

And we can use HttpClient to download the jwks file, then read it and convert to string format. Then we can use JwtSecurityTokenHandler to Validate Token. Like below:

var tokenHandler = new JwtSecurityTokenHandler();
try
{
    tokenHandler.ValidateToken(token, validationParameters, out var validatedToken);
    return validatedToken != null;
}

If you want more code details, you can refer to this post.

How to validate JWT Token using JWKS in Dot Net Core

Jason Pan
  • 15,263
  • 1
  • 14
  • 29