How to specify the key storage provider when importing a x509Certificate with private key

Viewed 16

In .Net 5/6, I got a RsaCng key in memory, got a certificate back from CA, how can I specify to use the "Microsoft Key Storage Provider" when importing to the Windows certificate store? I currently have the below code and it resulted in the CSP provider "Microsoft Enhanced RSA and AES Cryptographic Provider" once I import it into the certificate store. Setting the ProviderName to "Microsoft Key Storage Provider" gave an error of "The keyset is not defined".

                    var cspParams = new CspParameters
                    {
                        KeyContainerName = Guid.NewGuid().ToString(),
                        KeyNumber = (int)KeyNumber.Exchange,
                        Flags = CspProviderFlags.UseMachineKeyStore
                                | CspProviderFlags.UseNonExportableKey
                                | CspProviderFlags.NoPrompt,
                    };

                    var csp = new RSACryptoServiceProvider(cspParams);
                    csp.ImportParameters(myRsaCngKey.ExportParameters(true));
                    var certWithPrivateKey = cert.CopyWithPrivateKey(csp);
0 Answers
Related