Importing all certificates contained in a .p12 file

我怕爱的太早我们不能终老 提交于 2019-12-01 19:01:29

问题


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.


回答1:


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

You can then add each certificate to its appropiate store.



来源:https://stackoverflow.com/questions/7656324/importing-all-certificates-contained-in-a-p12-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!