public static X509Certificate2 GetCertificateFromStore2(string thumbprint) { foreach (StoreLocation storeLocation in (StoreLocation[])Enum.GetValues(typeof(StoreLocation))) { foreach (StoreName storeName in (StoreName[])Enum.GetValues(typeof(StoreName))) { X509Store store = new X509Store(storeName, storeLocation); try { store.Open(OpenFlags.ReadOnly); // Place all certificates in an X509Certificate2Collection object. X509Certificate2Collection certCollection = (X509Certificate2Collection)store.Certificates; // If using a certificate with a trusted root you do not need to FindByTimeValid, instead: // currentCerts.Find(X509FindType.FindBySubjectDistinguishedName, certName, true); X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); //X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByThumbprint, thumbprint, true); if (currentCerts.Count > 0) { X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, true); if (signingCert.Count > 0) // Return the first certificate in the collection, has the right name and is current. return signingCert[0]; } } finally { store.Close(); } } } return null; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)