I'm getting the following logged error pertaining to JWT creation and validation.
IDX10634: Unable to create the SignatureProvider.
Algorithm: 'HS256', SecurityKey: 'Microsoft.IdentityModel.Tokens.X509SecurityKey, KeyId: 'OzZ5Dbmcso9Qzt2ModGmihg30Bo', InternalId: 'OzZ5Dbmcso9Qzt2ModGmihg30Bo'.'
is not supported. The list of supported algorithms is available here: https://aka.ms/IdentityModel/supported-algorithms
Below is code for generating the JWT.
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_config["JwtSecret"]);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new System.Security.Claims.Claim("id", user.Email) }),
Expires = DateTime.UtcNow.AddHours(5),
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(key),
SecurityAlgorithms.HmacSha256Signature)
};
return tokenHandler.WriteToken(tokenHandler.CreateToken(tokenDescriptor));
Below is the code for validating the JWT.
var config = context.RequestServices.GetService<IConfiguration>();
var tokenHandler = new JwtSecurityTokenHandler();
var token = tokenHandler.ReadJwtToken(jwt);
_ = tokenHandler.ValidateToken(jwt, new()
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(config["JwtSecret"])),
ValidateIssuer = false,
ValidateAudience = false,
ClockSkew = TimeSpan.Zero // Tokens expire exactly at token expiration time
}, out var validatedToken);
What is wrong with the way we are creating the SigningCredentials? (We're using a supported algorithm)
Edit
It's coming clear that this error is coming from the framework, and not form our own parsing of the JWT. So, we're trying to figure out what's wrong with the JWT we're generating.
We are also getting this type of error:
IDX10503: Signature validation failed. Token does not have a kid. Keys tried: 'Microsoft.IdentityModel.Tokens.X509SecurityKey, KeyId: ....