相关疑难解决方法(0)

X509Store Certificates.Find 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,Find方法返回0 results(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
    { …
Run Code Online (Sandbox Code Playgroud)

c# wcf certificate ssl-certificate x509certificate

81
推荐指数
6
解决办法
5万
查看次数

如何在C#中通过指纹查找证书

我正在使用此代码通过其指纹查找证书.证书存在于个人证书存储区中,但此代码未找到该证书.

请告诉我我在哪里做错了.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string certThumbPrint = "??fe14593dd66b2406c5269d742d04b6e1ab03adb1";
            X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            // Try to open the store.

            certStore.Open(OpenFlags.ReadOnly);
            // Find the certificate that matches the thumbprint.
            X509Certificate2Collection certCollection = certStore.Certificates.Find(
                X509FindType.FindByThumbprint, certThumbPrint, false);
            certStore.Close();

            // Check to see if our certificate was added to the collection. If no, 
            // throw an error, if yes, create a certificate using it.
            if (0 == certCollection.Count)
            {
                Console.WriteLine("Error: No certificate found …
Run Code Online (Sandbox Code Playgroud)

c# x509certificate

78
推荐指数
4
解决办法
5万
查看次数

标签 统计

c# ×2

x509certificate ×2

certificate ×1

ssl-certificate ×1

wcf ×1