Error on Authentication net 6 Unable to match key

Viewed 320

I got the next error:

IDX10501: Signature validation failed. Unable to match key: 
kid: 'System.String'.
Exceptions caught:
 'System.Text.StringBuilder'. 
token: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.
   at Microsoft.IdentityModel.Tokens.InternalValidators.ValidateLifetimeAndIssuerAfterSignatureNotValidatedJwt(SecurityToken securityToken, Nullable`1 notBefore, Nullable`1 expires, String kid, TokenValidationParameters validationParameters, StringBuilder exceptionStrings)
   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)
   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()

I'm using Net 6 on a api with keycloak, my local conf was enter image description here I have a public kid on RS256 but I don't know how to add it to JwtBearer configuration, I'm new on this

I try add on appSettings like

enter image description here

all of this is on dev options, I got the key from keycloak realm

1 Answers

if you want to skip the authentication signature for jwt token keycloak and check later with a method you can use this in .net 6

In Program.cs

 builder.Services.AddAuthentication(option =>
 {
  option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
 }).AddJwtBearer(options =>
  {
   options.TokenValidationParameters = new TokenValidationParameters
   {
     ValidateIssuer = false,
     ValidateAudience = false,
     ValidateIssuerSigningKey = false,
     RequireSignedTokens = false,
     SignatureValidator = delegate (string token, TokenValidationParameters parameters) {
        var jwt = new JwtSecurityToken(token);

        return jwt;
      },
      ValidateLifetime = false,
      RequireExpirationTime = false,
      ClockSkew = TimeSpan.Zero,
    };
    options.Events = new JwtBearerEvents
     {
      OnTokenValidated = context =>
       {
        return Task.CompletedTask;
        },
    };
});
Related