2012-01-30 154 views
3

我试图枚举服务器上的证书存储并获取有关每个证书的信息。该代码正常工作,但缺少在“中级证书颁发机构”商店中找到的所有证书。枚举证书问题(X509Certificate2)

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 
        } 

      } 
+0

“中级证书颁发机构”的store.Certificates集合是否为空? – Hans 2012-01-30 20:21:49

回答

2

我最终得到它的工作,出于某种原因对每家商店,除了“CertificateAuthority”你可以通过名字,正如我在原来的代码做(店[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); 
2

使用 “CA”,而不是 “CertificateAuthority” 中间CA存储。 在MSDN中,它只列出了商店名称的枚举,但它们并不是真正正确的字符串供您传入。 查找正确的商店名称字符串的一种方法是先打开一个StoreName枚举的商店,然后检查store.Name值。