在Trusted Publishers商店中找不到证书

时间:2018-01-18 00:54:07

标签: c# x509certificate x509 x509certificate2

我想通过thumbprint在StoreLocation.LocalMachine中找到证书,但是,我发现如果我没有指定StoreName,我什么都得不到:

certStore = new X509Store(StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
     X509FindType.FindByThumbprint, thumbprint, false);
// always find nothing, no matter runs as Administrator or not.

但是如果我在初始化X509Store时指定StoreName,我可以找到证书。

certStore = new X509Store(StoreName.TrustedPublisher, StoreLocation.LocalMachine);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = certStore.Certificates.Find(
   X509FindType.FindByThumbprint, thumbprint, false);

MSDN没有说明X509Store构造函数之间的区别,如果未指定StoreName,是否意味着它会搜索所有商店名称?它看起来不像我的情况。

2 个答案:

答案 0 :(得分:2)

我查看了.NET源代码,<div class="container"> <div class="outer"> <div class="inner"> <canvas class="canvas"></canvas> </div> </div> <div class="outer"> <div class="inner"> <canvas class="canvas"></canvas> </div> </div> </div>在调用者未指定X509Store时使用StoreName.My。 MSDN没有对这种行为说一句话。

答案 1 :(得分:0)

如果您查看source code,如果您没有指定商店位置,请执行以下代码:

certStore = new X509Store(StoreLocation.LocalMachine);

然后构造函数将使用Store.My

public X509Store (StoreLocation storeLocation) : this ("MY", storeLocation) {}