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

本文关键字:证书 序列号 按序 存储 X509 | 更新日期: 2023-09-27 18:28:24

我需要通过序列号获得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/>");
                }
            }

以下是此代码的输出:

0091ED5F0CAED6AD52‎‎=0091ED5F0CAED6AD52
3D3233116A894CB244DB359DF99E7862=0091ED5F0CAED6AD52

很明显,我循环的第一个与序列号匹配,但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];

我在这里做错了什么?

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

您要查找的证书的证书序列号中似乎有两个不可见的字符,这就是它们不匹配的原因。如果您将foreach循环的输出更改为:,您应该能够确认这一点

System.Web.HttpContext.Current.Response.Write (string.Format("{0} (Length: {1}) = {2} (Length: {3})<br/>", cert.SerialNumber, cert.SerialNumber.Length oauthCertificateFindValue, oauthCertificateFindValue.Length);

您很可能会看到这些值看起来相同,但它们的长度不同(表示存在这些不可见的字符)。

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

您打开的第一个尚未关闭的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];
    }