Invalid signature of generated JWT

109 views Asked by At

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:

enter image description here

As a result I can't decode it: Jose.IntegrityException: 'Invalid signature.' What can I do to fix it?

0

There are 0 answers