从证书存储 C# MVC 获取 X509Certificate 列表

Get list of X509Certificate from cert store C# MVC

我正在尝试从证书存储中获取证书列表。这是我使用的代码 post Get list of certificates from the certificate store in C#:

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

当我 运行 Test Explorer 中的这段代码正在查找所有可用的证书时,但是当我 运行 它在我的 MVC 应用程序上时没有返回任何证书。 我是 运行宁 VS 2013 作为管理员。

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

编辑:

当我运行在 IIS Express 上运行代码时,我得到了证书列表,但是当我在本地 IIS 上运行它时,我没有得到任何结果。

此致,

也许你可以试试这个。

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

您可以使用此 link X509Store Class

中提供的示例迭代计算机上存在的商店位置和认证

大多数时候,您想检查机器存储证书,而不是当前用户的证书。为此:

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 中退出。

查看 How do I get the X509Certificate sent from the client in web service? 中评价最高的 IIS 代码答案。

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