Share via

Export Problem :(

OSVBNET 1,401 Reputation points
2022-05-19T07:26:54.78+00:00

Hello,
I'm not sure if I'm doing this right, since cannot export correctly:

Dim MyStore As X509Store = New X509Store(CertificateStoreName, StoreLocation.CurrentUser)
MyStore.Open(OpenFlags.ReadWrite)
Dim MyCertificate As X509Certificate2Collection = MyStore.Certificates.Find(X509FindType.FindByThumbprint, CertificateThumbprint, False)
If MyCertificate.Count > 0 Then
File.WriteAllBytes(FileTextBox.Text, MyCertificate(0).Export(X509ContentType.Pfx, PasswordTextBox.Text))
'OR
File.WriteAllBytes(FileTextBox.Text, MyCertificate(0).Export(X509ContentType.Pkcs7))
End If
MyStore.Close()

  1. If I export as .Pfx my exported cert will not contain the private key at all, can't figure it out.
  2. If I export as .Pkcs7 (.p7b ext) I always get Invalid content type error (export as .Cert works fine)
Developer technologies | VB
0 comments No comments

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2022-05-20T09:53:27.21+00:00

    Hi @OSVBNET ,
    The following link might be helpful.
    X509Certificate2.Export(X509ContentType, String) Method
    The contentType parameter accepts only the following values of the X509ContentType enumeration: Cert, SerializedCert, and Pkcs12. Passing any other value causes a CryptographicException to be thrown.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.