Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gbellmann
Last active September 2, 2015 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbellmann/6100b5a041d491db4626 to your computer and use it in GitHub Desktop.
Save gbellmann/6100b5a041d491db4626 to your computer and use it in GitHub Desktop.
Find X.509 certificate from store
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