【问题标题】:Cannot find certificate in Trusted Publishers store在受信任的发布者存储中找不到证书
【发布时间】:2018-01-18 00:54:07
【问题描述】:

我想通过指纹在 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,是不是意味着它会在所有商店名称中搜索?在我看来,情况并非如此。

【问题讨论】:

    标签: c# x509certificate x509 x509certificate2


    【解决方案1】:

    我查看了 .NET 源代码,当调用者未指定 StoreName 时,X509Store 使用 StoreName.My。 MSDN 对这种行为只字未提。

    【讨论】:

    • 默认值是多少?
    【解决方案2】:

    如果您查看source code,如果您没有指定商店位置,就像您下面的代码一样:

    certStore = new X509Store(StoreLocation.LocalMachine);
    

    那么构造函数将使用Store.My

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 2017-03-02
      • 2018-10-15
      • 1970-01-01
      相关资源
      最近更新 更多