I am trying to install a certificate on my local computer (Win Server 2003) with class X509Certificate2 in a C # console application. When I install the certificate with the following code, everything is fine:
var serviceRuntimeMachineCertificateStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine); serviceRuntimeMachineCertificateStore.Open(OpenFlags.ReadWrite); cert = new X509Certificate2(certificatePath); serviceRuntimeMachineCertificateStore.Add(cert); serviceRuntimeMachineCertificateStore.Close();
The problem is that the private key of the certificate is not saved when it is installed without X509KeyStorageFlags.PersistKeySet. Therefore, I tried to create such a certificate (the private key does not have a password, so I pass an empty string):
var serviceRuntimeMachineCertificateStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine); serviceRuntimeMachineCertificateStore.Open(OpenFlags.ReadWrite); cert = new X509Certificate2(certificatePath, "", X509KeyStorageFlags.PersistKeySet); serviceRuntimeMachineCertificateStore.Add(cert); serviceRuntimeMachineCertificateStore.Close();
But an attempt to initialize the certificate throws a System.Security.Cryptography.CryptographicException "Failed to load the certificate: the specified network password is incorrect", although the private key does not have a password.
If I import a certificate into the Microsoft Management Console without specifying a password, it works fine.
Does anyone know how to do this programmatically?
source share