12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography.X509Certificates;
- using System.Web;
- using System.Collections.Generic;
- namespace AipGateway.AIP
- {
- public static class Utilities
- {
- public static X509Certificate2 ReadCertificateFromStore(string thumbprint)
- {
- X509Certificate2 cert = null;
- X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
- store.Open(OpenFlags.ReadOnly);
- X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
- // Find unexpired certificates.
- X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
- // From the collection of unexpired certificates, find the ones with the correct name.
- X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false);
- // Return the first certificate in the collection, has the right name and is current.
- cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
- store.Close();
- return cert;
- }
- public static string EnsureTrailingSlash(string value)
- {
- if (value == null)
- {
- value = string.Empty;
- }
- if (!value.EndsWith("/", StringComparison.Ordinal))
- {
- return value + "/";
- }
- return value;
- }
- }
- }
|