Tyler
Tyler

Reputation: 173

Importing all certificates contained in a .p12 file

I have a .p12 file for my ASP.NET application to connect to a web service via HTTPS.

I am trying to import the .p12 file into the Local Machine/My store. The .p12 file contains more than one certificate. One contains the private key and the other is the CA certificate to complete the chain.

Currently I am using the `System.Security.Cryptography.X509Certificates.X509Certificate2 object's Import method to import this file into the store. Today I noticed that the CA certificate is not getting imported, only the main certificate containing the private key is getting imported using this method. After further review of MSDN I have found the following rule regarding the Import method:

Note that a PFX/PKCS12 certificate can contain more than one certificate. In that case, the first certificate associated with a private key is used or, if no private key is found, the first certificate is used.

Can anyone suggest to me another method for importing the .p12 file programmatically that will actually import all certificates in the file? I am using PowerShell to perform this function.

Upvotes: 4

Views: 7077

Answers (1)

Henning Krause
Henning Krause

Reputation: 5422

Instead of using the X509Certificate2.Import method, use the X509Certificate2Collection.Import method. It will give you all certificates from the .p12 file.

You can then add each certificate to its appropriate store.

Upvotes: 4

Related Questions