c# - X509Store Certificates.Find FindByThumbprint 问题

标签 c# wcf certificate ssl-certificate x509certificate

我在使用 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();
    }
}

最佳答案

我想您已将 Windows 证书信息对话框中的指纹复制粘贴到您的代码中(如果这是一个简化的示例,则复制粘贴到配置文件中)。恼人的是,指纹文本框中的第一个字符是 invisible Unicode "left-to-right-mark" control character .尝试选择开头的字符串引号和指纹的第一个字符,删除它们(这也将消除中间的不可见字符),然后手动重新键入它们。


今天我自己也遇到了这种奇怪的行为,我花了一个多小时才弄明白。我最终看到的方式是使用调试器检查证书对象的findValueThumbprint 的长度和哈希码,结果是不同的。这导致我在调试器中检查这些字符串的字符数组,其中出现了不可见字符。

关于c# - X509Store Certificates.Find FindByThumbprint 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448147/

相关文章:

java - 如何将证书链添加到 keystore ?

c# - 使用 lockbits 进行图像处理,替代 getpixel?

c# - 使用 CsvHelper 解析标题包含空格的 CSV

c# - 如何在c#中使用SQL Server存储过程数字参数

multithreading - WCF 如何处理多个客户端对服务器的调用?

c# - WCF 服务发现问题,需要多长时间才能返回结果?

node.js - NodeJS & SSL - "bad password read"

c# - 学习创建 ASP.NET SOAP Web 服务的良好起点

C# .NET 4.0 WCF MessageSecurityException,使用服务时出现 "Security header is empty."

certificate - 从 Windows Server 证书存储加载服务器端证书*和*私钥?