如何检索与C#一个PFX文件的证书? [英] How to retrieve certificates from a pfx file with c#?

查看:236
本文介绍了如何检索与C#一个PFX文件的证书?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索周围半天寻找一种方式来读取一个PFX文件并导入荣誉证书到的CertStore。

I've been googling around for half a day looking for a way to read a pfx file and import the certifcates into the certstore.

到目前为止,我能够读取X509Certifcate并能够在PFX文件PFX文件中导入一个证书。到目前为止好,但也有在总PFX文件三证和装载与X509证书的PFX的时候,我无法看到其他两个证书。

So far, i am able to read the pfx file with X509Certifcate and able to import one certificate within the pfx file. So far so good, but there are total of three certificates in the pfx file and when loading the pfx with X509Certificate, I am not able to see the other two certificates.

证书是出口

*个人信息交换 - PKCS#12(.PFX)

*Personal Information Exchange - PKCS #12 (.PFX)


  • 在包括如果可能,证书路径中所有证书

  • Include all certificates in the certification path if possible

启用加强保护(要求IE 5.0,NT 4.0 SP4或以上)

Enable strong protection (requires IE 5.0, NT 4.0 SP4 or above)

这些都是导出证书(S)时所选择的选项。我知道有三个荣誉证书,因为我手动进入的CertStore(MMC)和其导入到个人文件夹自己。

Those are the options selected when exporting the certificate(s). I know there are three certifcates because I manually go into the certstore (MMC) and import it into a personal folder myself.

任何投入将是AP preciated。

Any input would be appreciated.

感谢您

推荐答案

您应该能够获得通过X509Certificate2Collection类包含在你的.pfx文件的证书集合对象......这里的一些C#示例code

You should be able to get a collection object containing the certs in your .pfx file by using the X509Certificate2Collection class...here's some C# example code:

string certPath = <YOUR PFX FILE PATH>;
string certPass = <YOUR PASSWORD>;

// Create a collection object and populate it using the PFX file
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

然后你可以在集合迭代:

Then you can iterate over the collection:

foreach (X509Certificate2 cert in collection)
{
    Console.WriteLine("Subject is: '{0}'", cert.Subject);
    Console.WriteLine("Issuer is:  '{0}'", cert.Issuer);

    // Import the certificates into X509Store objects

根据证书(客户端证书,中级CA证书,根CA),你需要打开正确的证书存储(作为一个的X509Store对象),将其导入的类型。

Depending on the type of certificate (client cert, intermediate CA cert, root CA) you'll need to open the proper cert store (as an X509Store object) to import it.

退房的X509Store文档:

Check out the X509Store docs:

<一个href=\"http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx\">http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx

而在STORENAME枚举不同成员:

And the different members in the StoreName enumeration:

<一个href=\"http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx\">http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx

据我了解,你想用StoreName.My对于包含私钥,StoreName.CertificateAuthority中间CA证书,而StoreName.Root根CA证书客户端证书。

From what I understand, you want to use "StoreName.My" for client certificates that contain a private key, "StoreName.CertificateAuthority" for intermediate CA certs, and "StoreName.Root" for root CA certs.

这篇关于如何检索与C#一个PFX文件的证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆