2012-01-30 159 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值。