我无法使用自己创建的证书对JWT令牌进行签名。我了解签名(公钥/私钥)工作原理的基础,但是类和工具集却非常令人困惑。我已经花了很长时间来研究这个示例代码,现在我完全陷入了困境。当我运行附带的示例代码时,最终会出现以下错误(在tokenHandler.CreateToken()
调用中):
System.NotSupportedException:'IDX10634:无法创建 SignatureProvider。算法:“ [PII被隐藏]”,安全密钥:“ [PII被隐藏 隐藏]'不支持。'
我提供了在 CertificatePfxFileBase64Encoded 变量base64编码中创建的证书。这是一个.pfx文件,其中嵌入了私钥,没有密码。它是一个二进制文件,因此我必须对base64进行编码才能在此处共享。我试图对这些类的工作方式有一个很好的了解,但我无法克服这一障碍。有任何想法吗?下面的示例应该完全独立,您只需要收集引用即可。
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography.X509Certificates;
namespace SecurityStuff
{
public class Testing
{
static string CertificatePfxFileBase64Encoded = @"
MIIJqQIBAzCCCW8GCSqGSIb3DQEHAaCCCWAEgglcMIIJWDCCBA8GCSqGSIb3DQEHBqCCBAAwggP8
AgEAMIID9QYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQYwDgQI/SSYpMmSpVwCAggAgIIDyE7kByhH
OphDj2ZC39Zvlr2HWzXdXkMQnPEbuQ09d2B23iRW+UHJ+e2REf0UQqGRGTylL5nndtJS0zUvK+iq
dhjyM7NZs2h1gqoxixeI5JN8MQwvp2amxL+LSNG01qR+QDXuptJptP6DfpspbJR3dbpk0OvqENFF
q4QFItzvXbc+cF2ooF/rD6KArQj0mO/0IaTvaeSax680FtZj6IhHx5LPTBWGd+b6xZI60mhUL1qm
5iQ4VjPsduNzXsb8d6ISDiShLijkxgyqAeQBS8idLBTcP/6OXXr1hW78G3kcSw6HSA78xP4eKWQN
ZwJGnwBLUzVOD08Zkj2mNfcOjjEFArjclQjEnqjP2/AMYB7iRRwXIkrhs1hFuN3OJIMKXSd++LUk
Ujrtppta0cTfKEjnXEtIcBbTaQ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POVL8qtvzzJ6uKNcE2VwkaTaToPeUr3/jqIVC+dci74X8PdyFm2Bim7CdEBmo9uGv
gqh+fnvE1eWF3i+ihKqBVPHEGlZc/KSAr8rWb3MkB7/zJiq0C9ZseQNh30fxO+MR1GYxW9ZoiqmK
+xcfqUvg/0mxyQtjLXJU68VPwV8l9wOdpNVr5aNfAPZ/J49eyw2/dtK3ViQAQ2QkLSzELFpIiR21
D8cL8sbQ7b+Q5fzloG07VcyYQPfxxJjHJCKSGsWWLV+OiYVh063nnLdSp3E2YlN5XJbXHYftQJBg
82TRU4QSy6q4zm1tfdD2FgWKIZbJmwbx0pGx4ftgQyIcVW/NHj5ej9dMunGevBEhqBBwvZ4aAKhp
jHzS9qoHBDjUL9PCLCsQHG6NS7HK6Nc3AbuIyV1asMlJeisJTWMy6M1B1JOIWcCV3ChY7o3Cfgee
3nqvVVT1Nh8NpqUPYm6/+XjbNufTeRM8IqB7gi5TOEDnJrECH0Se1EPnpiJZucSKkKUxJTAjBgkq
hkiG9w0BCRUxFgQU/EYyZZVcNLJF4k8lAbQVGZXQSygwMTAhMAkGBSsOAwIaBQAEFE5XVOAJU286
B1gJ8j4mJ7HmXhEABAjfmw/u2zlmsAICCAA=";
public static string CreateTokenWithX509SigningCredentials()
{
var signingCert = new X509Certificate2(Convert.FromBase64String(CertificatePfxFileBase64Encoded));
var privateKey = new X509SecurityKey(signingCert);
var algo = privateKey.PrivateKey.SignatureAlgorithm;
var signingCredentials = new SigningCredentials(privateKey, algo);
var now = DateTime.Now;
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.Name, "John"),
new Claim(ClaimTypes.Role, "Sales"),
}),
Issuer = "self",
IssuedAt = now,
NotBefore = now,
Expires = now.AddMinutes(50),
//SigningCredentials = new X509SigningCredentials(signingCert), //For some reason this class no longer exists
SigningCredentials = signingCredentials,
};
var tokenHandler = new JwtSecurityTokenHandler();
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
string tokenString = tokenHandler.WriteToken(token);
return tokenString;
}
}
}
====编辑====
我认为显示我如何制作签名证书可能会有所帮助,因为问题可能出在那儿。下面是我用来生成证书 CertificatePfxFileBase64Encoded 的命令行。我使用应用程序openSSL来生成它。
openssl req -new -newkey rsa:2048 -nodes -keyout CA_PrivateKey.key -out CA_SigningRequest.csr
openssl x509 -req -days 2000 -in CA_SigningRequest.csr -signkey CA_PrivateKey.key -out CA_Certificate.crt
openssl pkcs12 -export -out CA_Certificate.pfx -inkey CA_Private.key -in CA_Certificate.crt
答案 0 :(得分:4)
以下代码从.pfx文件生成JWT令牌。
public static string GenerateToken(int expireMinutes)
{
X509Certificate2 signingCert = new X509Certificate2("PFXFilePath", "password");
X509SecurityKey privateKey = new X509SecurityKey(signingCert);
var now = DateTime.UtcNow;
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Expires = now.AddMinutes(Convert.ToInt32(expireMinutes)),
SigningCredentials = new SigningCredentials(privateKey, SecurityAlgorithms.RsaSha256Signature)
};
JwtSecurityToken stoken = (JwtSecurityToken)tokenHandler.CreateToken(tokenDescriptor);
string token = tokenHandler.WriteToken(stoken);
return token;
}
答案 1 :(得分:0)
SigningCredentials
的第二个参数必须是URI。
您可以为此使用枚举SecurityAlgorithms
。
注意:要查看[隐藏的PII]字段,可以使用:Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;