【发布时间】:2023-04-05 22:50:01
【问题描述】:
我们有几个测试会生成一个 jwt 请求来调用服务器来检索令牌。我们有 6 个测试使用相同的数据对相同的方法进行相同的调用。这是方法: '''
private static string GenerateSignedTokenRequest(
string privateKey,
string privateKeyPass,
string clientID,
string audience,
int lifetime)
{
var jti = Guid.NewGuid().ToString();
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Jti, jti),
new Claim(JwtRegisteredClaimNames.Sub, clientID),
};
var decodedKey = DecodeRsaPrivateKeyFromPem(
privateKey,
privateKeyPass);
var priDecKey = decodedKey.Private as RsaPrivateCrtKeyParameters;
var rsaParams = DotNetUtilities.ToRSAParameters(priDecKey);
using (var rsa = RSA.Create(rsaParams))
{
var token = new JwtSecurityToken(
clientID,
audience,
claims,
DateTime.Now.AddMinutes(-1),
DateTime.Now.AddSeconds(lifetime),
new SigningCredentials(
new RsaSecurityKey(rsa),
SecurityAlgorithms.RsaSha256));
return new JwtSecurityTokenHandler().WriteToken(token);
}
}
'''
在 WriteToken(token) 方法上运行的所有其他测试中,我们都会收到以下错误: {"无法访问已释放的对象。\r\n对象名称:'RSA'。"}
令人困惑的是,每个奇数测试都可以很好地运行此代码,但每个偶数测试都失败了。但是当我单独重新运行每个测试时,它们都是绿色的。只有当我将它们全部一起运行时,其他所有测试都失败了。
从 .Net Core 和测试框架从 3.1.0 迁移到 3.1.4 时发生这种情况
【问题讨论】:
-
不确定你的意思。这在 Visual Studio 中编译得很好。结束括号就在分号之前。 var token = new JwtSecurityToken(clientID, Audience, Claims, DateTime.Now.AddMinutes(-1), DateTime.Now.AddSeconds(lifetime), new SigningCredentials( new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256));
-
啊,我错过了。