X509Store 证书问题.查找 FindByThumbprint [英] Problems with X509Store Certificates.Find FindByThumbprint

查看:38
本文介绍了X509Store 证书问题.查找 FindByThumbprint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用方法 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 设为常量,则该方法会找到证书.

In this case the Find Method returns 0 results (results.Count == 0), but if I put the findValue as constant the method find the certificate.

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 证书信息对话框中的指纹复制粘贴到您的代码中(如果这是一个简化示例,则复制粘贴到配置文件中).令人讨厌的是,指纹文本框中的第一个字符是 不可见的 Unicodeleft-to-right-mark"" 控制字符.尝试选择开头的字符串引号和指纹的第一个字符,删除它们(这也将去除中间的不可见字符),然后手动重新键入.

I suppose that you have copy-pasted the thumbprint from the Windows certificate information dialog box into your code (or to a config file if this is a simplified example). Annoyingly, the first character in the thumbprint textbox is the invisible Unicode "left-to-right-mark" control character. Try selecting the opening string quote and the first character of the thumbprint, deleting them (which will also get rid of the invisible character in-between), and retyping them by hand.

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

I was subjected to this odd behaviour myself today, and it took me over an hour to figure it out. The way I finally saw it was by using the debugger to check the lengths and hash codes of findValue and of the Thumbprint of the certificate object, which turned out to be different. This led me to inspect the character arrays of those strings in the debugger, where the invisible character showed up.

这篇关于X509Store 证书问题.查找 FindByThumbprint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆