title: RSA Disposed Object Error - every other test
body: <p>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:
  '''</p>
  
  <pre><code>    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);
          }
  
      }
  </code></pre>
  
  <p>'''</p>
  
  <p>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'."}</p>
  
  <p>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. </p>
  
  <p>This has happened when moving from .Net Core and Test frameworks from 3.1.0 to 3.1.4</p>
  
  <p><a href="https://i.stack.imgur.com/N8LNm.png" rel="noreferrer">enter image description here</a></p>