X509Store证书存在问题。FindByThumbprint查找方法

99

我在使用X509Store.Certificates.Find方法时遇到了问题。

public static X509Certificate2 FromStore(StoreName storeName, 
          StoreLocation storeLocation, X509FindType findType, string findValue)
{
    X509Store store = new X509Store(storeName, storeLocation);
    store.Open(OpenFlags.ReadOnly);
    try
    {
        //findValue = "7a6fa503ab57b81d6318a51ca265e739a51ce660"
        var results = store.Certificates.Find(findType, findValue, true);

        return results[0];                
    }
    finally
    {
        store.Close();
    }
}
在这种情况下,Find方法返回0个结果(results.Count == 0),但是如果我将findValue设置为常量,该方法会找到证书。
public static X509Certificate2 FromStore(StoreName storeName, 
           StoreLocation storeLocation, X509FindType findType, string findValue)
{
    X509Store store = new X509Store(storeName, storeLocation);
    store.Open(OpenFlags.ReadOnly);
    try
    {         
        //findValue= "7a6fa503ab57b81d6318a51ca265e739a51ce660"
        var results = store.Certificates.Find(findType, 
                              "7a6fa503ab57b81d6318a51ca265e739a51ce660", true);
        return results[0];
    }
    finally
    {
        store.Close();
    }
}
14个回答

0
经过长时间的分析,以下是对我有效的方法:
  1. 将证书中的指纹复制到记事本中。
  2. 将记事本中的指纹复制到Visual Studio中。
  3. 以管理员身份运行Visual Studio。

这个方法非常有效。


0

只是想让你知道这个看不见的字符是什么,我在mmc中看到指纹是:75 3a ...

然后我将其复制并粘贴到我的vim中,我看到以下内容:

<200e>75 3a ...

所以在你去掉第一个字符"<200e>"和多余的空格之后,一切都会好的。


0
var results = store.Certificates.Find(findType, findType, true);

我认为你的第二个参数应该是“findValue”。


第二个参数实际上是findValue。 - nunofamel
如果是这种情况,那么问题就在其他地方了。除非实际内容不同(空格?尾随换行符?),否则字面字符串与字符串变量参数不会像这样中断。 - Joe

0

+1 对 Aasmund Eldhuset 的回答(以及其他答案)。

令人烦恼的是,指纹文本框中的第一个字符是不可见的Unicode“从左到右标记”控制字符。

很难验证它是否存在。例如,将指纹从我的配置文件复制到VS二进制编辑器时,有时会得到不可见字符,有时则不会。

此外,这段代码未能显示问题。我逐步执行代码并在 x509Store 上悬停以找到所需的证书。

                X509Certificate2 cert2 = null;
                string storeName = StoreName.My.ToString();
                var x509Store = new X509Store(storeName, StoreLocation.LocalMachine);
                x509Store.Open(OpenFlags.ReadOnly);

                var cert3 = x509Store.Certificates[4];
                var thumbprint3 = cert3.Thumbprint;
                int gotIt = thumbprint3.CompareTo(clientCert);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接