X509Certificate.Export Method

I am trying to export a .pfx certificate:

string certPath = "D:\\cert.pfx";
cert = new X509Certificate2(certPath, "pass");

byte[] certData = cert.Export(X509ContentType.Pfx,"pass"); /// **error in this line** 

X509Certificate newCert = new X509Certificate(certData,"pass");

But it ends with this error:

The key is not valid for use in the specified state.

Can someone help me? Any solution as an export certificate from the store

+3
source share
3 answers

I think that you could not export the private key, because the constructor that you used to create the certificate X509Certificate2(filePath, password)does not mark the received certificate as exportable. I suggest using one of the overloaded constructors, which allows you to specify the exported flag - X509KeyStorageFlags.Exportable, for example X509Certificate2(filePath, password, X509KeyStorageFlags.Exportable).

X509Certificate2 X509KeyStorageFlags MSDN.

X509Certificate2.HasPrivateKey, , , .

+8

, ?

.

EDIT:

, , .

0

, , :

pfx

byte[] certData = certificado.Export(X509ContentType.Pfx, "pass");

try to save as another type of content, mine was "Cert"

byte[] certData = certificado.Export(X509ContentType.Cert, "pass");
0
source

Source: https://habr.com/ru/post/1717415/


All Articles