我有可以从加密狗安装的证书 . 当我使用IE查看安装的证书时 . 它告诉我它有私钥如下 .
但是当我尝试使用我的c#代码将该证书导出到.pfx时,私钥为null .
下面是我将证书导出到pfx的代码,其中私钥始终为null
public static bool ExportCertificateToPFX(string certificateSerialNumber, string pxfFilepath, StoreName storeName, StoreLocation location)
{
bool success = false;
X509Store store = new X509Store(storeName, location);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates.Find(X509FindType.FindBySerialNumber, certificateSubject, true);
if (certs != null && certs.Count > 0)
{
//BELOW CONDITION ALWAYS FALSE
if (certs[0].HasPrivateKey)
{
MessageBox.Show(certs[0].PrivateKey.ToString());
}
byte[] data = certs[0].Export(X509ContentType.Pfx, "KALPESH");
//HERE WRITE "data" TO "pxfFilepath" FILE
}
store.Close();
return success;
}