枚举证书颁发(X509Certificate2)
本文关键字:X509Certificate2 证书 枚举 | 更新日期: 2023-09-27 18:21:59
我正在尝试枚举服务器上的证书存储,并获取有关每个证书的信息。该代码工作正常,只是缺少在"中间证书颁发机构"存储中找到的所有证书。
string[] stores = new string[] { "AddressBook", "AuthRoot", "CertificateAuthority", "Disallowed", "My", "Root", "TrustedPeople", "TrustedPublisher" };
for (int x = 0; x < stores.Length; x++)
{
X509Store store = new X509Store(stores[x],StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 mCert in store.Certificates)
{
//handle certificates
}
}
我最终让它工作了,出于某种原因,除了"CertificateAuthority"之外,每个商店都可以像我在原始代码(stores[x])中那样传递名称。对于"CertificateAuthority",我必须明确地传递"Store.CertificateAuthority"。我觉得这是X509Store类中的一个错误。
//Old Code
string[] stores = new string[] { "AddressBook", "AuthRoot", "CertificateAuthority" "Disallowed", "My", "Root", "TrustedPeople", "TrustedPublisher" };
X509Store store = new X509Store(stores[x],StoreLocation.LocalMachine);
//New Code
X509Store store2= new X509Store(StoreName.CertificateAuthority, StoreLocation.LocalMachine);
对中间CA存储使用"CA"而不是"CertificateAuthority"。在MSDN中,它只列出了存储名称的枚举,但它们并不是真正适合您传入的字符串。找到正确的商店名称字符串的一种方法是首先打开具有StoreName枚举的商店,然后检查该商店。名称值。