2016-04-21 100 views
1

我創建了下面列出的C#.Net控制檯程序來掃描所有證書存儲並顯示證書信息。問題是它沒有顯示所有證書。掃描證書存儲不顯示所有證書

例如,此命令行顯示在個人存儲證書:

CERTUTIL.EXE -store My 

但是我的測試程序顯示其爲無個人證書。我正在使用Windows 2008 R2。這是簡化的控制檯應用程序。任何想法我可能做錯了什麼?我在普通的CMD窗口中嘗試了兩種方式,並且以相同的結果作爲管理員。

using System; 
using System.Linq; 
using System.Security.Cryptography.X509Certificates; 
using System.Collections; 

namespace CertView 
{ 
    class Program 
    { 
     static int Main(string[] args) 
     { 
      var stores = Enum.GetValues(typeof(StoreName)); 
      IEnumerator enumStores = stores.GetEnumerator(); 
      foreach (StoreName sn in stores) 
      { 
       X509Store str = new X509Store(sn.ToString()); 
       str.Open(OpenFlags.ReadOnly); 
       int count = str.Certificates.Count; 
       if (count > 0) 
       { 
        Console.WriteLine("Store: " + sn.ToString() + Environment.NewLine); 
        foreach (X509Certificate2 x509 in str.Certificates) 
        { 
         Console.WriteLine("Friendly name: {0}", x509.FriendlyName); 
         Console.WriteLine("Issued to: " + x509.GetNameInfo(X509NameType.SimpleName, false)); 
         Console.WriteLine("Issued by: " + x509.GetNameInfo(X509NameType.SimpleName, true)); 
         Console.WriteLine("Thumbprint: " + x509.Thumbprint); 
         x509.Reset(); 
        } 
       } 
       str.Close(); 
      } 
     } 
    } 
} 

回答

2

這是因爲certutil默認側重於LocalMachine店,而X509Store側重於CurrentUser店。 X509Store構造函數的讀註釋部分:https://msdn.microsoft.com/en-us/library/h16bc8wd(v=vs.110).aspx

您需要在指定商店位置的地方使用不同的構造函數。例如,這一個:X509Store(StoreName,StoreLocation)

+0

輝煌。謝謝! –

+1

@NeilWeicher使用certutil列出CurrentUser \我的證書運行命令'certutil -user- store my' – pepo

+0

謝謝。你如何明確指定LocalMachine,還是僅僅是默認的?我嘗試了-LocalMachine,-System和其他一些。 –