Steve Guidi Steve Guidi - 1 year ago 238
ASP.NET (C#) Question

How do I validate a JWT using JwtSecurityTokenHandler and a JWKS endpoint?

I am prototyping the use IdentityServer4 to secure several services, with the caveat that those services will likely not be migrated (in the forseeable future) to use the OWIN middleware idiom of ASP.NET Core. Consequently, I can not leverage the many middleware helpers that automate the validation of a JWT by simply providing the well-known JWKS endpoint of IdentityServer, among other things.

It would be nice if I could reconstruct this behavior, and I'd like to leverage Microsoft's

implementation if possible. However, I can not figure out how to utilize the
types provided via IdentityServer's discovery endpoint to extract keys and perform the validation.

to validate a JWT, and those parameters require an instance of one or more
objects to perform the validation.

ClaimsPrincipal ValidateJwt(string token, IdentityModel.Client.DiscoveryResponse discovery)
JwtSecurityToken jwt = new JwtSecurityToken(token);

TokenValidationParameters validationParameters = new TokenValidationParameters
ValidateAudience = true,
ValidateIssuer = true,
RequireSignedTokens = true,
ValidIssuer = "expected-issuer",
ValidAudience = "expected-audience",
IssuerSigningKeys = discovery.KeySet.Keys /* not quite */

JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
SecurityToken validatedToken;
return handler.ValidateToken(jwt, validationParameters, out validatedToken);

How do I perform the necessary translation from
so that the validation can occur? Is there another method (apart from OWIN middleware) that will also work using the
data above?

(Sadly, the documentation for
is not up to date.)

Answer Source

Check this sample:

It manually retrieves the key from the JWK and populates the validation parameters.