RSA 处置对象错误 - 每隔一个测试 [英] RSA Disposed Object Error - every other test

查看:28
本文介绍了RSA 处置对象错误 - 每隔一个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个测试会生成一个 jwt 请求来调用服务器来检索令牌.我们有 6 个测试,它们使用相同的数据对相同的方法进行相同的调用.这是方法:'''

We have several tests that that generate a jwt request to call a server to retrieve a token. We have 6 tests that make the same call to the same method using the same data. Here is the method: '''

    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) 方法上运行的每个其他测试中,我们都会收到以下错误:{"无法访问已处理的对象. 对象名称:'RSA'."}

We get the following error on every other test that runs on the WriteToken(token) method: {"Cannot access a disposed object. Object name: 'RSA'."}

令人困惑的是,每个奇数测试都可以很好地运行此代码,但每个偶数测试都失败了.但是当我单独重新运行每个测试时,它们都是绿色的.只有当我一起运行它们时,所有其他测试才会失败.

What is baffling is each odd number test runs through this code fine but each even number test fails. But when I rerun each test individually they are all green. It is only when I run them all together that every other test fails.

从 .Net Core 和 Test 框架从 3.1.0 迁移到 3.1.4 时会发生这种情况

This has happened when moving from .Net Core and Test frameworks from 3.1.0 to 3.1.4

在此处输入图片描述

推荐答案

所以问题似乎是升级了 Windows Azure Active Directory IdentityModel Extensions for .Net.似乎有一个缓存不受在 RSA.Create() 方法周围放置 using 的影响.通过删除 using,所有测试都是绿色的.

So It seems the issue was the upgrade of Windows Azure Active Directory IdentityModel Extensions for .Net. It seems there is a cache that not is affected by putting a using around the RSA.Create() method. By removing the using all the tests are green.

这里有一些帮助我诊断的链接:https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc

here are a few links that helped my diagnose: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc

还有:

https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/issues/1433

这篇关于RSA 处置对象错误 - 每隔一个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆