从cert store C#MVC获取X509证书列表

从cert store C#MVC获取X509证书列表,c#,windows,model-view-controller,x509certificate,x509certificate2,C#,Windows,Model View Controller,X509certificate,X509certificate2,我正在尝试从证书库获取证书列表。这是我在这篇文章中使用的代码: 当我从测试资源管理器运行此代码时,它会查找所有可用的证书,但当我在MVC应用程序上运行它时,它不会返回任何证书。 我以管理员身份运行VS 2013 你能告诉我我做错了什么吗 编辑: 当我在IIS Express上运行代码时,我会得到证书列表,但当我在本地IIS上运行代码时,我不会得到任何结果 您好,您可以试试这个 X509Store store = new X509Store(StoreLocation.CurrentUser);

我正在尝试从证书库获取证书列表。这是我在这篇文章中使用的代码:

当我从测试资源管理器运行此代码时,它会查找所有可用的证书,但当我在MVC应用程序上运行它时,它不会返回任何证书。 我以管理员身份运行VS 2013

你能告诉我我做错了什么吗

编辑:

当我在IIS Express上运行代码时,我会得到证书列表,但当我在本地IIS上运行代码时,我不会得到任何结果


您好,

您可以试试这个

X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 mCert in store.Certificates)
{
    // TODO
}

您可以使用此链接上提供的示例来迭代存储位置和计算机上存在的证书

大多数情况下,您希望检查计算机存储证书,而不是当前用户的证书。为此:

X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
    // TODO
}

这将为您提供一个一致的列表,而不考虑IIS用户。

如果您试图接受来自用户的证书,则需要正确配置IIS以使用HTTPS并接受来自客户端的SSL。如果不对代码进行一些更改,您将无法从IIS Express和IIS 8.0开始

在中查看IIS代码的最高评分答案


对于IIS Express,您无法配置SSL设置,因此如果要伪抓取x509属性,可以从本地存储中进行。看起来您现在正在做的就是这样,这在本地IIS上不起作用,因为ApplicationPoolIdentity没有访问证书存储的特权。

我使用了这个X509Store存储=新的X509Store(StoreName.My,StoreLocation.CurrentUser);结果是一样的。
X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
foreach (X509Certificate2 certificate in store.Certificates)
{
    // TODO
}