Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find certificate by thumbprint

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.

like image 617
Nininea Avatar asked Jan 29 '26 23:01

Nininea


1 Answers

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;
    }
like image 51
Adam Labi Avatar answered Jan 31 '26 11:01

Adam Labi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!