Last active
September 2, 2015 16:01
-
-
Save gbellmann/6100b5a041d491db4626 to your computer and use it in GitHub Desktop.
Find X.509 certificate from store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public X509Certificate2 FindCertificate(StoreLocation storeLocation, StoreName storeName, X509FindType findType, object searchCriteria) | |
{ | |
X509Store certificateStore = new X509Store(storeName, storeLocation); | |
certificateStore.Open(OpenFlags.ReadOnly); | |
X509Certificate2Collection certificates = certificateStore.Certificates; | |
X509Certificate2Collection matchingCertificates = certificates.Find(findType, searchCriteria, false); | |
if (matchingCertificates != null && matchingCertificates.Count > 0) | |
{ | |
return matchingCertificates[0]; | |
} | |
certificateStore.Close(); | |
throw new ArgumentException("Unable to find a matching certificate in the certificate store. Please modify the search criteria."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment