Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static X509Certificate2 GetCertificate(string thumbPrint)
- {
- List<StoreLocation> locations = new List<StoreLocation>
- {
- StoreLocation.CurrentUser,
- StoreLocation.LocalMachine
- };
- foreach (var location in locations)
- {
- X509Store store = new X509Store(StoreName.My, location);
- try
- {
- store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
- X509Certificate2Collection certificates = store.Certificates.Find(
- X509FindType.FindByThumbprint, thumbPrint, true);
- if (certificates.Count >= 1)
- {
- return certificates[0];
- }
- }
- finally
- {
- store.Close();
- }
- }
- throw new ArgumentException($"A Certificate with Thumbprint '{thumbPrint}' could not be located.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement