【发布时间】:2021-10-13 16:16:59
【问题描述】:
在 Azure API 管理策略表达式中我需要创建一个使用私钥签名的 JWT。
当我尝试使用 RSACryptoServiceProvider - 只是为了检查这个 feedback 是否已经得到解决 - 我在尝试保存策略时收到此错误:
Usage of type 'System.Security.Cryptography.RSACryptoServiceProvider' is not supported within expressions
根据maxim-kim 的提示,我尝试了 RSA.Create() 并从该tutorial 转换
var privateKey = "whatever";
RSA rsa = RSA.Create();
rsa.ImportRSAPrivateKey(privateKey, out _);
var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256)
{
CryptoProviderFactory = new CryptoProviderFactory { CacheSignatureProviders = false }
};
var now = DateTime.Now;
var unixTimeSeconds = new DateTimeOffset(now).ToUnixTimeSeconds();
var jwt = new JwtSecurityToken(
audience: _settings.Audience,
issuer: _settings.Issuer,
claims: new Claim[] {
new Claim(JwtRegisteredClaimNames.Iat, unixTimeSeconds.ToString(), ClaimValueTypes.Integer64),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(nameof(claims.FirstName), claims.FirstName),
new Claim(nameof(claims.LastName), claims.LastName),
new Claim(nameof(claims.Email), claims.Email)
},
notBefore: now,
expires: now.AddMinutes(30),
signingCredentials: signingCredentials
);
string token = new JwtSecurityTokenHandler().WriteToken(jwt);
return new JwtResponse
{
Token = token,
ExpiresAt = unixTimeSeconds,
};
但出现下一个错误:
'RSA' does not contain a definition for 'ImportRSAPrivateKey' and no extension method 'ImportRSAPrivateKey' accepting a first argument of type 'RSA' could be found (are you missing a using directive or an assembly reference?)
所以我的问题是:有没有办法在 Azure API 管理策略表达式中创建签名的 JWT?
【问题讨论】:
标签: azure jwt azure-api-management