Find certificate by thumbprint

10,633

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;
    }
Share:
10,633
Nininea
Author by

Nininea

Xamarin Mobile Developer (Certified): Android platform IOS platform .Net Software Developer

Updated on June 05, 2022

Comments

  • Nininea
    Nininea almost 2 years

    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.