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.
Try false as your third parameter to the store.Certificates.Find() method - its possible your your certificates are not valid and are being excluded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With