gpt4 book ai didi

c# - 如何使用 C# 从 pfx 文件中检索证书?

转载 作者:IT王子 更新时间:2023-10-29 04:21:20 32 4
gpt4 key购买 nike

我在谷歌上搜索了半天,寻找一种方法来读取 .pfx 文件并将证书导入 certstore

到目前为止,我能够使用X509Certifcate 读取.pfx 文件,并能够在.pfx 文件中导入一个证书。到目前为止还不错,但是 .pfx 文件中有三个证书,当使用 X509Certificate 加载 .pfx 时,我无法查看其他两个证书。

证书是用

导出的

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

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

  • 启用强保护(需要 IE 5.0、NT 4.0 SP4 或更高版本)

这些是导出证书时选择的选项。我知道有三个证书,因为我手动进入 certstore (MMC) 并将其导入到个人文件夹中。

最佳答案

您应该能够通过使用 X509Certificate2Collection 类在您的 .pfx 文件中获取包含证书的集合对象...这里是一些 C# 示例代码:

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);

然后你可以遍历集合:

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 对象)才能导入它。

查看 X509Store 文档:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx

StoreName 枚举中的不同成员:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx

据我了解,您想将 StoreName.My 用于包含私钥的客户端证书,将 StoreName.CertificateAuthority 用于中间 CA 证书,并将 StoreName.Root 用于根 CA 证书。

关于c# - 如何使用 C# 从 pfx 文件中检索证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5036590/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com