【问题标题】:Scanning certificates store does not show all certificates扫描证书存储不显示所有证书
【发布时间】:2016-08-15 04:07:44
【问题描述】:

我创建了一个下面列出的 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();
            }
        }
    }
}

【问题讨论】:

    标签: c# windows certificate x509certificate


    【解决方案1】:

    这是因为certutil 默认关注LocalMachine 存储,而X509Store 关注CurrentUser 存储。阅读X509Store构造函数的备注部分:https://msdn.microsoft.com/en-us/library/h16bc8wd(v=vs.110).aspx

    您需要在指定存储位置的位置使用不同的构造函数。比如这个:X509Store(StoreName, StoreLocation)

    【讨论】:

    • 太棒了。谢谢!
    • @NeilWeicher 要使用 certutil 列出 CurrentUser\My 证书,请运行命令 certutil -user- store my
    • 谢谢。您如何明确指定 LocalMachine,或者这只是默认值?我尝试了 -LocalMachine、-System 和其他一些。
    • 在 certutil 中,如果“-user”开关没有出现,在所有情况下都默认使用本地机器。
    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2021-07-31
    • 2011-11-09
    • 2010-10-24
    • 2010-10-01
    相关资源
    最近更新 更多