Last active
July 22, 2020 02:13
-
-
Save timdenholm/ac4af57829b405e2d68fede7a8d1e1c2 to your computer and use it in GitHub Desktop.
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
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