且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有x5c的情况下从jwks验证JWT的签名

更新时间:2022-11-03 20:34:08

使用x5c只是一种方法,但是您也可以使用参数e(公共指数)和n(模数)来检索公钥. ),也记录在 jose-jwt github页面中:

Using x5c is just one way, but you can also retrieve the public key with the parameters e (public exponent) and n (modulus), which is also documented on the jose-jwt github page:

//If kid was found then load public key
if (jwkkey != null)
{
    RSACryptoServiceProvider key = new RSACryptoServiceProvider();
    key.ImportParameters(new RSAParameters
    {
        Modulus = Base64Url.Decode(jwkkey.n),
        Exponent = Base64Url.Decode(jwkkey.e)
    });
}

// get the public key as Base64Url encoded string, e.g. to use it on jwt.io
var pubkey = Base64Url.Encode(key.ExportRSAPublicKey());

var o = Jose.JWT.Decode(jsonToken.RawData, key);

您还可以再次将公共密钥导出为Base64Url编码的字符串,如上面的代码所示,然后稍后使用该密钥在

You can also export the public key as Base64Url encoded string again as shown in the code above, and later use that key to manually verify your token on https://jwt.io