Utilities.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. using System.Security.Cryptography.X509Certificates;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. namespace AipGateway.AIP
  8. {
  9. public static class Utilities
  10. {
  11. public static X509Certificate2 ReadCertificateFromStore(string thumbprint)
  12. {
  13. X509Certificate2 cert = null;
  14. X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  15. store.Open(OpenFlags.ReadOnly);
  16. X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
  17. // Find unexpired certificates.
  18. X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
  19. // From the collection of unexpired certificates, find the ones with the correct name.
  20. X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false);
  21. // Return the first certificate in the collection, has the right name and is current.
  22. cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
  23. store.Close();
  24. return cert;
  25. }
  26. public static string EnsureTrailingSlash(string value)
  27. {
  28. if (value == null)
  29. {
  30. value = string.Empty;
  31. }
  32. if (!value.EndsWith("/", StringComparison.Ordinal))
  33. {
  34. return value + "/";
  35. }
  36. return value;
  37. }
  38. }
  39. }