Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. static X509Certificate2 GetCertificate(string thumbPrint)
  2. {
  3. List<StoreLocation> locations = new List<StoreLocation>
  4. {
  5. StoreLocation.CurrentUser,
  6. StoreLocation.LocalMachine
  7. };
  8.  
  9. foreach (var location in locations)
  10. {
  11. X509Store store = new X509Store(StoreName.My, location);
  12. try
  13. {
  14. store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
  15. X509Certificate2Collection certificates = store.Certificates.Find(
  16. X509FindType.FindByThumbprint, thumbPrint, true);
  17. if (certificates.Count >= 1)
  18. {
  19. return certificates[0];
  20. }
  21. }
  22. finally
  23. {
  24. store.Close();
  25. }
  26. }
  27.  
  28. throw new ArgumentException($"A Certificate with Thumbprint '{thumbPrint}' could not be located.");
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement