我试图枚举服务器上的证书存储,并获取有关每个证书的信息。该代码正常工作,只是缺少“中间证书颁发机构”存储中找到的所有证书。
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);