简体   繁体   中英

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);

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

X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM