我正在尝试以编程方式在本地计算机的证书存储库中导入X509证书(pfx / PKCS#12)。这个特定的证书有一个证书链,证书路径如下所示:
CA
- Organization 2 certificate CA
- My certificate
我使用的代码如下所示:
cert = new X509Certificate2(pathToCert, password);
if (cert != null)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
if (!store.Certificates.Contains(cert))
{
store.Add(cert);
}
}
此代码确实导入了证书,但是它似乎忽略了链。如果我检查存储中的证书,则证书路径仅显示:
我的证书
但是,当我手动导入pfx时,它确实显示了完整路径。我在这里是跳过了一个步骤,还是我遗漏了一些参数?谁能解释一下这件事?
发布于 2012-02-06 04:43:48
通过将PFX文件作为X509Certificate2Collection对象打开,您应该能够迭代PFX中的证书(并将每个证书导入到您选择的证书存储中)。
以下是有关X509Certificate2Collection的文档:
MSDN在文档页面中提供了一些示例代码,介绍如何检查集合中每个证书。
一旦知道了每个证书的CNs/颁发者/其他信息,就应该清楚每个证书需要添加到哪个证书存储中。为此,您可以使用X509Store类和StoreName枚举来指定要打开/添加到哪个存储:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store.aspx
http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx
另请看我对一个类似的SO问题的回答:
How to retrieve certificates from a pfx file with c#?
正如该答案的最新评论之一所提到的,当您尝试将证书导入到当前用户的根存储区("StoreName.Root“和"StoreLocation.CurrentUser”作为名称/位置)时,您将看到一个弹出对话框,要求您进行确认。
为了解决这个问题,我只在证书导入方法中添加了一小段MS UI自动化代码,在提示符上单击OK。
或者,正如评论者"CodeWarrior“在另一个答案的评论中所说的那样,为了避免弹出对话框,您可以尝试将根证书放入LocalMachine存储中,而不是CurrentUser中。
示例代码:
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 certificate into an X509Store object
}
发布于 2012-02-13 20:40:37
为了便于将来参考,我发现了另一种方法,使用X509Chain对象:
var cert = new X509Certificate2(pathToCert, password);
X509Chain chain = new X509Chain();
chain.Build(cert);
for (int i = 0; i < chain.ChainElements.Count; i++)
{
//add to the appropriate store
}
发布于 2016-01-08 12:55:44
对于任何想要“到适当的商店”代码通用解决方案的人
这是我用VB创建的,所以移植到C#应该不难。我使用上面的帖子来开始我的工作,我在这方面完全是一个NooB。
Dim certPath = "C:\Users\08353153\Documents\Visual Studio 2015\Projects\WindowsApplication2\WindowsApplication2\bin\Debug\8870-thebigchess.pfx"
Dim certPass = "eduSTAR.NET"
Dim Collection As New X509Certificate2Collection
Collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet)
Dim certOne As X509Certificate2 = Collection(0)
Dim certTwo As X509Certificate2 = Collection(2)
Dim certThree As X509Certificate2 = Collection(1)
Dim personal As New X509Store(StoreName.My, StoreLocation.LocalMachine)
personal.Open(OpenFlags.ReadWrite)
personal.Add(certOne)
personal.Close()
Dim trust As New X509Store(StoreName.Root, StoreLocation.LocalMachine)
trust.Open(OpenFlags.ReadWrite)
trust.Add(certTwo)
trust.Close()
Dim intermed As New X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine)
intermed.Open(OpenFlags.ReadWrite)
intermed.Add(certThree)
intermed.Close()
https://stackoverflow.com/questions/9141198
复制相似问题