C#jwt ECDSA P256

时间:2018-10-23 02:05:08

标签: c#

我们的合作伙伴为我们提供了一个需要验证的JWT令牌。该令牌的算法为ES256,它们还为我们提供了一个用于验证令牌的公共密钥。我陷入了这个问题。

JWT示例:

  

eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJzbSIsImV4cCI6MTUzMzEyNTk1NywianRpIjoidU51U2U5aENianM2Y3F3NmxqRlRBZHZRbmdMRlNRTFZxdUVndWJodWRqeCtNbElxM0U0Y3plM1VUbDdZdmdWZCIsImlhdCI6MTUzMzEyMjM1NywiaXNzIjoiZ3IiLCJuYmYiOjE1MzMxMjIzNTcsImlkIjoiR0VUR08xMDBRV0VSMTIzNDA5ODcyMzQ1Njc4OTAwOTg3NjUxMjM0IiwidXJzIjoiQVdBUkRFRCJ9.Vu-v5KgisesxsZSp1gkcQBOquHRGB-CWQ79PpcdEGSWItZCiTn0NfWKspUnsPAgQ4x9icLcKhhjNqPCRAosYDQ

示例键:

  

----- BEGIN公钥-----   MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEndLYzAXirR4avOpgFy2Ei49su09w   6i / nnRB5o22hNUyfu9nccM5DWFuMKf4JH8c2GleQDcwveT1mN6btANWnRg ==   ----- END公钥-----

using (ECDsaCng dsa = new ECDsaCng(y))
        {
            dsa.HashAlgorithm = CngAlgorithm.Sha256;
            var unsignedJwtData =  Base64UrlEncode(hea) + "." + 
            Base64UrlEncode(jwtPayload);
            var s = Base64UrlEncode(signature);
            byte[] b = Encoding.ASCII.GetBytes(unsignedJwtData);
            byte[] ss = Encoding.ASCII.GetBytes(s);
            Console.WriteLine(dsa.VerifyData(b, ss).ToString());
        }

如何验证上面的示例令牌?谢谢!

0 个答案:

没有答案