Inserting Certificate (with privatekey) in Root, LocalMachine certificate store fails in .NET 4

后端 未结 6 516
清酒与你
清酒与你 2020-11-30 08:12

I\'m having problems inserting a new CA certificate with privatekey in the Root certificate store of the localmachine.

This is what happens:

//This          


        
6条回答
  •  一整个雨季
    2020-11-30 08:27

    I had exactly the same problem and the solution turned out to be really simple. All I had to do is to pass

    X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet
    

    to X509Certificate2's ctor. Now you are using the DotNetUtilities to convert the bouncycastle certificate to the .net one, but the helper method creates the .net cert with the DefaultKeySet (instead of MachineKeySet + PersistKeySet ).

    And arrange the private key like this:

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

    I hope this helps.

提交回复
热议问题