我在使用 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 .尝试选择开头的字符串引号和指纹的第一个字符,删除它们(这也将消除中间的不可见字符),然后手动重新键入它们。
今天我自己也遇到了这种奇怪的行为,我花了一个多小时才弄明白。我最终看到的方式是使用调试器检查证书对象的findValue
和Thumbprint
的长度和哈希码,结果是不同的。这导致我在调试器中检查这些字符串的字符数组,其中出现了不可见字符。
关于c# - X509Store Certificates.Find FindByThumbprint 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8448147/