【发布时间】:2017-03-15 00:25:31
【问题描述】:
我想使用我自己提供的非对称密钥在 .Net 4.5 中生成 JWT 令牌,但我遇到了 System.IdentityModel.Tokens.Jwt 版本 4.0.3 的一些问题。
最好我会创建自己的 2048 密钥,就像提供商允许我做的那样。 RSA.Create() 构造函数创建 1024 个密钥。
using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider(2048))
{
var publicPrivate = provider.ToXmlString(true);
var publicKeyOnly = provider.ToXmlString(false);
var stuff = provider.ExportParameters(true);
signingCredentials = new SigningCredentials(new RsaSecurityKey(RSA.Create()), SecurityAlgorithms.RsaSha256Signature, SecurityAlgorithms.Sha256Digest); //no idea how to pull the key out of here.
}
在许多示例中,可以将 RSAParameters 放入 RsaSecurityKey 构造函数中,但现在它只需要 RSA.Create() 构造函数(带有可选的字符串参数) 以下代码片段来自https://stackoverflow.com/a/38233644 请注意,在此示例中,RSAParameters 转到很好地进入 RsaSecurityKey 构造函数,我不能用我的版本做,我被限制使用 RSA.Create,它接缝。
// NOTE: Replace this with your actual RSA public/private keypair!
var provider = new RSACryptoServiceProvider(2048);
var parameters = provider.ExportParameters(true);
// Build the credentials used to sign the JWT
var signingKey = new RsaSecurityKey(parameters); //not an option for me, unfortunately
【问题讨论】:
-
其中一个元素是,对于 4.0.3,不使用 RsaSecurityKey 中的参数,您使用提供程序。 var provider = new RSACryptoService(2048); var signingKey = new RsaSecurityKey(provider);我在以下链接中找到了它。 github.com/AzureAD/…
标签: c# .net jwt encryption-asymmetric