Find Certificate by hash in Store C#

Cobaia picture Cobaia · Jul 26, 2011 · Viewed 8.3k times · Source

How to get Certificate by hash in Windows Store using C#?

sha1 example:7a0b021806bffdb826205dac094030f8045d4daa

this loop works but:

X509Store store = new X509Store(StoreName.My);

store.Open(OpenFlags.ReadOnly);

foreach (X509Certificate2 mCert in store.Certificates)
{
    Console.WriteLine( mCert.Thumbprint);
}

store.Close();

Is there a direct method?

Answer

Bob Vale picture Bob Vale · Jul 26, 2011
var cert = store.Certificates.Find(
                                    X509FindType.FindByThumbprint,
                                    thumbprint,
                                    true
                                  ).OfType<X509Certificate>().FirstOrDefault();