Skip to content

Instantly share code, notes, and snippets.

@timdenholm
Last active July 22, 2020 02:13
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 timdenholm/ac4af57829b405e2d68fede7a8d1e1c2 to your computer and use it in GitHub Desktop.
Save timdenholm/ac4af57829b405e2d68fede7a8d1e1c2 to your computer and use it in GitHub Desktop.
using System.Security.Cryptography.X509Certificates;
// GetCertificate
// Gets a certificate from the user's certificate store that matches
// the specified serial number
static X509Certificate2 GetCertificate(string serialNumber)
{
using (var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
certStore.Open(OpenFlags.ReadOnly);
var certCollection = certStore.Certificates
.Find(X509FindType.FindBySerialNumber, serialNumber, true);
X509Certificate2 certificate = null;
if (certCollection.Count > 0)
{
certificate = certCollection[0];
Console.WriteLine("Found a matching certificate");
}
else
{
Console.WriteLine("No matching certificate found");
}
return certificate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment