Utilities.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Collections.Generic;
  5. using Microsoft.InformationProtection;
  6. using Microsoft.InformationProtection.Protection;
  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. public static AipLabel LabelToAip(Label label)
  39. {
  40. if (label == null) return null;
  41. return new AipLabel
  42. {
  43. Id = label.Id,
  44. Name = label.Name,
  45. Sensitivity = label.Sensitivity,
  46. Description = label.Description,
  47. IsActive = label.IsActive,
  48. ActionSource = (AipActionSource)label.ActionSource,
  49. Children = new List<AipLabel>()
  50. };
  51. }
  52. public static AipTemplate TemplateToAip(TemplateDescriptor template)
  53. {
  54. if (template == null) return null;
  55. var aipTemplate = new AipTemplate
  56. {
  57. Id = template.Id,
  58. Name = template.Name,
  59. Description = template.Description
  60. };
  61. return aipTemplate;
  62. }
  63. }
  64. }