RSA处置对象错误-其他测试 [英] RSA Disposed Object Error - every other test

查看:79
本文介绍了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)方法上运行的所有其他测试中,我们都会收到以下错误: {无法访问已处置的对象.\ r \ n对象名称:'RSA'."}

We get the following error on every other test that runs on the WriteToken(token) method: {"Cannot access a disposed object.\r\nObject 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()方法的影响而受到影响.通过删除使用,所有测试均为绿色.

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.

以下一些链接可以帮助我进行诊断:

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天全站免登陆