Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X509Certificate2 has private key not exportable?

On Win Server 2008, I can execute the code below, and when I try to export the cert via the MMC GUI, I do not have the option to export the private key as well. However, if I use the GUI to import the cert, I am allowed to export the private key also. What am I missing in my code?

string certfile = @"mycert.p12";
SecureString secString = new SecureString();
foreach (char c in "password")
{
    secString.AppendChar(c);
}

X509Certificate2 cert = new X509Certificate2(certfile, secString, X509KeyStorageFlags.Exportable);
var store = new X509Store(StoreName.My , StoreLocation.CurrentUser);
store.Add(cert);
like image 272
Snowy Avatar asked Nov 16 '10 20:11

Snowy


1 Answers

Aha. The key storage flags should be Exportable and persisted.

X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet
like image 115
Snowy Avatar answered Oct 13 '22 13:10

Snowy