I use jose-jwt library for encoding and decoding JWT tokens.
Here's how I create payload and encode it:
var payload = new Dictionary<string, object>
{
{ "id", Id },
{ "username", Name },
{ "roles", string.Join(",", RolesIds) },
{ "iat", new DateTimeOffset(Issued).ToUnixTimeSeconds().ToString() },
{ "exp", ExpireSeconds.ToString() }
};
var privateKeyBytes = Encoding.UTF8.GetBytes(appSettings.PrivateKey);
var cngKey = new ECDsaCng(CngKey.Create(CngAlgorithm.ECDsaP256));
cngKey.SignData(privateKeyBytes, 0, privateKeyBytes.Length, HashAlgorithmName.SHA256);
var token = Jose.JWT.Encode(payload, cngKey, Jose.JwsAlgorithm.ES256);
But generated token has invalid signature:
As a result I can't decode it:
Jose.IntegrityException: 'Invalid signature.'
What can I do to fix it?
