Purpose : Generate a ES256 signed JWT using jose-jwt
Steps:
1.Generate a private key and certificate using openssl:
openssl ecparam -name prime256v1 -genkey > privateKey.pem
openssl req -new -key privateKey.pem -x509 -nodes -days 365 -out public.cer
2.Token generation:
var payload = new Dictionary<string, object>()
{
{ "sub", "mr.x@contoso.com" },
{ "exp", 1300819380 }
};
var certificate = X509Certificate.CreateFromCertFile("public.cer");
byte[] publicKey = certificate.GetPublicKey(); //public key has 65 bytes
//Below step is throwing an error:
var cng = CngKey.Import(publicKey, CngKeyBlobFormat.EccPublicBlob);
var token = JWT.Encode(claims, cng, JwsAlgorithm.ES256);
CngKey.Import() is throwing "The parameter is incorrect" error while trying to generate a CngKey required for the Jose.JWT.Encode function. Not sure what step I am missing. Thanks.