Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 扫描证书存储不会显示所有证书_C#_Windows_Certificate_X509certificate - Fatal编程技术网

C# 扫描证书存储不会显示所有证书

C# 扫描证书存储不会显示所有证书,c#,windows,certificate,x509certificate,C#,Windows,Certificate,X509certificate,我创建了一个下面列出的C#.Net控制台程序来扫描所有证书存储并显示证书信息。问题是它没有显示所有证书 例如,此命令行显示个人存储中的证书: CERTUTIL.EXE -store My 然而,我的测试程序显示它没有个人证书。我正在使用Windows 2008 R2。这里是缩写的控制台应用程序。知道我做错了什么吗?我在一个普通的CMD窗口中进行了尝试,并作为管理员进行了尝试,结果相同 using System; using System.Linq; using System.Security.

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

这是因为默认情况下,
certutil
侧重于
LocalMachine
store,而
X509Store
侧重于
CurrentUser
store。阅读
X509Store
constructor上的备注部分:


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

。谢谢@NeilWeicher要使用certutil列出CurrentUser\My证书,请运行命令
certutil-user-store My
谢谢。如何显式指定LocalMachine,或者这只是默认值?我尝试了-LocalMachine、-System和其他一些方法。在certutil中,如果不显示“-user”开关,则在所有情况下都默认使用本地机器。