在代码中向商店添加X509证书 [英] Add an X509 certificate to a store in code
问题描述
此代码将在证书存储中添加一个x509 cer证书文件(使用 System.Security.Cryptography.X509Certificates
):
This code will add a x509 cer cert file into the certificate store (using System.Security.Cryptography.X509Certificates
):
var filename = "Cert.cer";
var cert = new X509Certificate2(filename);
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
证书已通过以下方式生成:
Where the certificate has been generated with:
makecert -r -pe -sky exchange -n "CN=Blah" Cert.cer -sv Cert.pvk
但是 - 这会将证书添加到当前用户的个人证书 - 如何将证书添加到不同的证书集合 - 在我的情况下,我想添加到当前用户的受信任的人证书。
But - this will add the certificate into the "Personal" certificates of the currentuser - how can I add the certificate to a different collection of certificates - in my case I want to add to the "Trusted People" certificates for currentuser.
感谢
推荐答案
var store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
第一个参数包含要使用的商店的枚举,请参阅MSDN
The First Parameter contains the enumeration for which store to use see MSDN
第二个参数包含要使用的位置的枚举(例如,计算机,当前用户)请参见 MSDN
The Second Parameter contains the enumeration for which location to use (eg Computer, Current user) see MSDN
这篇关于在代码中向商店添加X509证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!