Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CertificateStore's Certificates.Find() doesn't actually find the certificate

Tags:

c#

certificate

Here's my simple method:

private static X509Certificate2 GetCertificateFromStore(StoreLocation storeLocation, string certName) {
    var store = new X509Store(StoreLocation.LocalMachine);
    try {
        store.Open(OpenFlags.ReadOnly);
        var certs = store.Certificates.Find(X509FindType.FindBySubjectName, certName, true);
        return certs.Count == 0 ? null : certs[0];
    }
    finally {
        store.Close();
    }
}

Debug locals show that store.Certificates has been loaded and contains two certificates — the default "localhost" one and one I've imported, so the correct store has been successfully opened.

However, the Find() method always returns an empty result, regardless of which certificate I search for and whether I use FindBySubjectName or FindByThumbprint.

Any ideas what could be wrong? It is a simple console app created for the sole purpose of learning & testing certificate loading, i.e., virtually nothing in project configuration or anywhere else is other than default.

like image 432
Modus Operandi Avatar asked Nov 15 '25 18:11

Modus Operandi


1 Answers

Try false as your third parameter to the store.Certificates.Find() method - its possible your your certificates are not valid and are being excluded.

like image 81
Chris A Avatar answered Nov 18 '25 09:11

Chris A