C# X509存储无法按序列号找到证书

C# X509存储无法按序列号找到证书,c#,x509certificate,x509certificate2,C#,X509certificate,X509certificate2,我需要获得一个X509证书的序列号,我有序列号,我循环通过他们,我看到了我需要的收集序列号,但从来没有找到 这是我的调试代码,只是为了确保看到正确的序列号: X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadOnly); foreach (X509Certificate2 cert in store

我需要获得一个X509证书的序列号,我有序列号,我循环通过他们,我看到了我需要的收集序列号,但从来没有找到

这是我的调试代码,只是为了确保看到正确的序列号:

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            foreach (X509Certificate2 cert in store.Certificates)
            {
                System.Web.HttpContext.Current.Response.Write (cert.SerialNumber + "=" + oauthCertificateFindValue + "<br/>");
                if (cert.SerialNumber == oauthCertificateFindValue)
                {
                    System.Web.HttpContext.Current.Response.Write("<br/>FOUND FOUND FOUND<br/>");
                }
            }
很明显,我循环的第一个与序列号匹配,但是
if
总是失败,并且基于该序列号我真正需要做的工作也失败:

   X509Certificate2Collection certificateCollection = store.Certificates.Find(x509FindType, oauthCertificateFindValue, false);

   if (certificateCollection.Count == 0)
                {
                    throw new ApplicationException(string.Format("An OAuth certificate matching the X509FindType '{0}' and Value '{1}' cannot be found in the local certificate store.", oauthCertificateFindType, oauthCertificateFindValue));
                }

     return certificateCollection[0];

我在这里做错了什么?

您打开的第一个
X509Store
尚未关闭,您试图从中获取已读取的证书

首先关闭要匹配序列的代码中的存储区和要从存储区读取的代码,然后重新打开存储区

下面的代码为我工作。只要核实一下

 private static void CompareCertSerialse()
    {
        string oauthCertificateFindValue = "7C00001851CBFF5E9F563E236F000000001851";

        X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

        store.Open(OpenFlags.ReadOnly);

        foreach (X509Certificate2 cert in store.Certificates)
        {
            Console.WriteLine(cert.SerialNumber + "=" + oauthCertificateFindValue);
            if (cert.SerialNumber == oauthCertificateFindValue)
            {
              Console.WriteLine("FOUND FOUND FOUND>");

             //Close the store  
              store.Close();
              GetCert(oauthCertificateFindValue);                     
            }
        }
    }


 private static X509Certificate2 GetCert(string oauthCertificateFindValue)
    {
        //Reopen the store
        X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);

        X509Certificate2Collection certificateCollection = store.Certificates.Find(X509FindType.FindBySerialNumber, oauthCertificateFindValue, false);

        if (certificateCollection.Count == 0)
        {
            Console.WriteLine("Nothing Found");
        }

        return certificateCollection[0];
    }

您试图查找的证书的证书序列号中似乎有两个不可见的字符,这就是它们不匹配的原因。如果将
foreach
循环的输出更改为:

System.Web.HttpContext.Current.Response.Write (string.Format("{0} (Length: {1}) = {2} (Length: {3})<br/>", cert.SerialNumber, cert.SerialNumber.Length oauthCertificateFindValue, oauthCertificateFindValue.Length);
System.Web.HttpContext.Current.Response.Write(string.Format(“{0}(长度:{1})={2}(长度:{3})
”,cert.SerialNumber,cert.SerialNumber.Length oauthCertificateFindValue,oauthCertificateFindValue.Length);
您很可能会看到这些值看起来相同,但它们的长度不同(表示存在这些不可见字符)


您需要更新搜索值以匹配证书的序列号,包括不可见字符。

看起来没有问题。
x509FindType是什么?
?您是否尝试更改它?您是否有可能将序列号从标准的Windows证书查看对话框中复制出来?我以前发现过类似的问题从对话框复制时,会在复制字符串的开头插入一个不可见字符,导致查找失败。请检查
oauthCertificateFindValue.Length
是否为序列号的预期长度(如果序列号表示为十六进制字符串,则可能会被2整除).我通过循环检查证书并输出它们来寻找我特别需要的证书,从而获得了序列号
System.Web.HttpContext.Current.Response.Write (string.Format("{0} (Length: {1}) = {2} (Length: {3})<br/>", cert.SerialNumber, cert.SerialNumber.Length oauthCertificateFindValue, oauthCertificateFindValue.Length);