3

System can't find certificate by string thumbprint

       var thumbprint = "‎2E7F6E8A0124E6745C3999EE15770C0A4931F342";
        X509Certificate2 certificate = new X509Certificate2();
        X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
        store.Open(OpenFlags.OpenExistingOnly);
        var c = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false).OfType<X509Certificate>().FirstOrDefault();

this core returns null. But I try this too

foreach (X509Certificate2 mCert in store.Certificates)
        {

           var c= store.Certificates.Find(X509FindType.FindByThumbprint, mCert.Thumbprint, false).Count;
        }

c is always 1 , so some problem is in characters. I copied this thumbprint value.

Nininea
  • 2,671
  • 6
  • 31
  • 57
  • Is there a leading space in the thumbprint value? For some reason in some cases it displays one - in my experience - if you use the certificate snap-in, but that is NOT part of the thumbprint. – qqbenq May 15 '14 at 07:39
  • this is value var thumbprint="‎2E7F6E8A0124E6745C3999EE15770C0A4931F342" – Nininea May 15 '14 at 07:43
  • 1
    See this question , maybe it can help: http://stackoverflow.com/questions/8448147/problems-with-x509store-certificates-find-findbythumbprint – qqbenq May 15 '14 at 07:46
  • Below link will help you https://stackoverflow.com/questions/11115511/how-to-find-certificate-by-its-thumbprint-in-c-sharp – Ashutosh B Bodake Jun 26 '17 at 12:54

1 Answers1

12

The following works for me:

    public async Task<X509Certificate2> GetCertificate(string certificateThumbprint)
    {
        var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);
        var cert = store.Certificates.OfType<X509Certificate2>()
            .FirstOrDefault(x => x.Thumbprint == certificateThumbprint);
        store.Close();
        return cert;
    }
Adam Labi
  • 436
  • 7
  • 16