Pultroppo no, ho gia provato.
Questo è il codice che utilizzo, potrebbe essere un problema di lunghezza della stringa _secret?
private const string _secret = "xxx";
public static string CreateTokenForIdentity(Login identity)
{
byte[] key = Convert.FromBase64String(_secret);
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(key);
SecurityTokenDescriptor descriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Email, identity.mail),
new Claim(ClaimTypes.Role, identity.admin.ToString())
}),
Expires = DateTime.UtcNow.AddMinutes(120),
SigningCredentials = new SigningCredentials(securityKey,
SecurityAlgorithms.HmacSha256Signature)
};
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
JwtSecurityToken token = handler.CreateJwtSecurityToken(descriptor);
return handler.WriteToken(token);
}
public static IPrincipal getIdentity(string token)
{
JwtSecurityTokenHandler tokenHandler = new JwtSecurityTokenHandler();
JwtSecurityToken jwtToken = (JwtSecurityToken)tokenHandler.ReadToken(token);
if (jwtToken == null)
return null;
byte[] key = Convert.FromBase64String(_secret);
TokenValidationParameters parameters = new TokenValidationParameters()
{
RequireExpirationTime = true,
ValidateIssuer = false,
ValidateAudience = false,
ValidateIssuerSigningKey = false,
IssuerSigningKey = new SymmetricSecurityKey(key)
};
SecurityToken securityToken;
ClaimsPrincipal principal = tokenHandler.ValidateToken(token, parameters, out securityToken);
Thread.CurrentPrincipal = principal;
return principal;
}
Viene generata un'eccezione nella validazione.
"Message": "An error has occurred.",
"ExceptionMessage": "IDX10503: Signature validation failed. Keys tried: 'System.Text.StringBuilder'.\nExceptions caught:\n 'System.Text.StringBuilder'.\ntoken: 'System.IdentityModel.Tokens.Jwt.JwtSecurityToken'.",
"ExceptionType": "Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException"
Grazie
Tonio