用的X509Store Certificates.Find FindByThumbprint问题 [英] Problems with X509Store Certificates.Find FindByThumbprint

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

问题描述

我有一个问题,当我使用的方法 X509Store.Certificates.Find

 公共静态X509Certificate2 FromStore(STORENAME STORENAME,
StoreLocation storeLocation,X509FindType findType,串findValue)
{
的X509Store店=新的X509Store(STORENAME,storeLocation);
store.Open(OpenFlags.ReadOnly);

{
// findValue =7a6fa503ab57b81d6318a51ca265e739a51ce660
VAR的结果= store.Certificates.Find(findType,findValue,真正的);

返回结果[0];
}
终于
{
store.Close();
}
}

在这种情况下,查找方法返回0的结果( results.Count == 0 ),但如果我把findValue恒定的方法查找证书。

 公共静态X509Certificate2 FromStore(STORENAME STORENAME,
storeLocation storeLocation,X509FindType findType,串findValue)
{
的X509Store店=新的X509Store(STORENAME,storeLocation);
store.Open(OpenFlags.ReadOnly);

{
// findValue =7a6fa503ab57b81d6318a51ca265e739a51ce660
VAR的结果= store.Certificates.Find(findType,
7a6fa503ab57b81d6318a51ca265e739a51ce660,真正的);
返回结果[0];
}
终于
{
store.Close();
}
}


解决方案

我假设你的例子是有点简化,而且你真的加载 findValue 从配置文件或类似的东西。如果您有复制粘贴从证书信息对话框配置文件的指纹(或者一个字符串中的代码,对于这个问题的文字),它可能是第一个字符是无形的Unicode左到右-标记。尝试删除整个字符串在配置文件中,的包括引号的,并手动重新键入。



我经受这种奇怪的行为自己今天,我花了一个多小时的数字出来。我终于看到它的方式是通过使用调试器来检查 findValue 的长度和哈希码和的指纹该证书的对象。


I'm having a problem when I use the method 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();
    }
}

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();
    }
}

解决方案

I suppose that your example is somewhat simplified, and that you are really loading findValue from a config file or something similar. If you have copy-pasted the thumbprint from the certificate information dialog box to the config file (or to a string literal in the code, for that matter), it is possible that the first character is the invisible Unicode "left-to-right-mark". Try deleting the entire string in the config file, including the quotes, and retyping it by hand.

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.

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

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