title: "CryptographicException: Invalid provider type specified" only in WebAPI project
body: <p>I have the following code for accessing a secret in KeyVault using a certificate installed on the local machine:</p>
  <pre><code>static readonly string certThumbprint = &quot;1234...&quot;;
  static readonly string clientId = &quot;1234...&quot;;
  
  static void Main(string[] args)
  {
      X509Certificate2 certificate = FindCertificateByThumbprint(certThumbprint);
      ClientAssertionCertificate assertionCert = new ClientAssertionCertificate(clientId, certificate);
  
      var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
              (authority, resource, scope) =&gt; GetAccessToken(authority, resource, scope, assertionCert)));
  
  
      string url = &quot;https://myvault.vault.azure.net/&quot;;
      var result = keyVaultClient.GetSecretAsync(url, &quot;mySecret&quot;).Result;
  }
  
  private static X509Certificate2 FindCertificateByThumbprint(string certificateThumbprint)
  {
      if (certificateThumbprint == null)
      {
          throw new System.ArgumentNullException(&quot;CertificateThumbprint&quot;);
      }
  
      StoreLocation[] storeLocations = (StoreLocation[])Enum.GetValues(typeof(StoreLocation));
  
      foreach (StoreLocation location in storeLocations)
      {
          foreach (StoreName storeName in (StoreName[])
              Enum.GetValues(typeof(StoreName)))
          {
              X509Store store = new X509Store(storeName, location);
  
              store.Open(OpenFlags.ReadOnly);
  
              X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
  
              if (certCollection != null &amp;&amp; certCollection.Count != 0)
              {
                  foreach (X509Certificate2 cert in certCollection)
                  {
                      if (cert.HasPrivateKey)
                      {
                          store.Close();
                          return cert;
                      }
                  }
              }
          }
      }
      throw new Exception($&quot;Could not find the certificate with thumbprint {certificateThumbprint} in any certificate store.&quot;);
  }
  
  private static async Task&lt;string&gt; GetAccessToken(string authority, string resource, string scope, ClientAssertionCertificate assertionCert)
  {
      Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority, TokenCache.DefaultShared);
      AuthenticationResult result = await context.AcquireTokenAsync(resource, assertionCert).ConfigureAwait(false);
  
      return result.AccessToken;
  }
  </code></pre>
  <p>This works fine in my .Net Framework 4.7.2 console app with the following packages installed:</p>
  <pre><code>  &lt;ItemGroup&gt;
      &lt;PackageReference Include=&quot;Microsoft.Azure.KeyVault&quot;&gt;
        &lt;Version&gt;3.0.5&lt;/Version&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;Microsoft.IdentityModel.Clients.ActiveDirectory&quot;&gt;
        &lt;Version&gt;5.2.8&lt;/Version&gt;
      &lt;/PackageReference&gt;
      &lt;PackageReference Include=&quot;System.Security.Cryptography.X509Certificates&quot;&gt;
        &lt;Version&gt;4.3.2&lt;/Version&gt;
      &lt;/PackageReference&gt;
    &lt;/ItemGroup&gt;
  </code></pre>
  <p>However, when I run the exact same code on my WebAPI project (.Net Framework 4.7.2), I get the following error:</p>
  <pre><code>&quot;ClassName&quot;: &quot;System.Security.Cryptography.CryptographicException&quot;,
  &quot;Message&quot;: &quot;Invalid provider type specified.\r\n&quot;
  </code></pre>
  <p>The packages I've installed are the same version according to Nuget:</p>
  <pre><code>&lt;Reference Include=&quot;Microsoft.Azure.KeyVault, Version=3.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL&quot;&gt;
    &lt;HintPath&gt;..\packages\Microsoft.Azure.KeyVault.3.0.5\lib\net461\Microsoft.Azure.KeyVault.dll&lt;/HintPath&gt;
  &lt;/Reference&gt;
  &lt;Reference Include=&quot;Microsoft.IdentityModel.Clients.ActiveDirectory, Version=5.2.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL&quot;&gt;
    &lt;HintPath&gt;..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.5.2.8\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll&lt;/HintPath&gt;
  &lt;/Reference&gt;
  &lt;Reference Include=&quot;System.Security.Cryptography.X509Certificates, Version=4.1.1.2, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL&quot;&gt;
    &lt;HintPath&gt;..\packages\System.Security.Cryptography.X509Certificates.4.3.2\lib\net461\System.Security.Cryptography.X509Certificates.dll&lt;/HintPath&gt;
    &lt;Private&gt;True&lt;/Private&gt;
    &lt;Private&gt;True&lt;/Private&gt;
  &lt;/Reference&gt;
    
  </code></pre>
  <p>I guess that some other package is causing a conflict and resolving some incorrect version of one of the needed libraries but I'm unsure how to diagnose &amp; resolve.</p>