Utilities.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. using Microsoft.InformationProtection;
  8. using Microsoft.InformationProtection.Protection;
  9. namespace AipGateway.AIP
  10. {
  11. public static class Utilities
  12. {
  13. public static X509Certificate2 ReadCertificateFromStore(string thumbprint)
  14. {
  15. X509Certificate2 cert = null;
  16. X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  17. store.Open(OpenFlags.ReadOnly);
  18. X509Certificate2Collection certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
  19. // Find unexpired certificates.
  20. X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
  21. // From the collection of unexpired certificates, find the ones with the correct name.
  22. X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindByThumbprint, thumbprint, false);
  23. // Return the first certificate in the collection, has the right name and is current.
  24. cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
  25. store.Close();
  26. return cert;
  27. }
  28. public static string EnsureTrailingSlash(string value)
  29. {
  30. if (value == null)
  31. {
  32. value = string.Empty;
  33. }
  34. if (!value.EndsWith("/", StringComparison.Ordinal))
  35. {
  36. return value + "/";
  37. }
  38. return value;
  39. }
  40. public static AipLabel LabelToAip(Label label)
  41. {
  42. if (label == null) return null;
  43. var aipLabel = new AipLabel
  44. {
  45. Id = label.Id,
  46. Name = label.Name,
  47. Sensitivity = label.Sensitivity,
  48. Description = label.Description,
  49. IsActive = label.IsActive,
  50. ActionSource = (AipActionSource)label.ActionSource,
  51. Children = new List<AipLabel>()
  52. };
  53. return aipLabel;
  54. }
  55. public static AipTemplate TemplateToAip(TemplateDescriptor template)
  56. {
  57. if (template == null) return null;
  58. var aipTemplate = new AipTemplate
  59. {
  60. Id = template.Id,
  61. Name = template.Name,
  62. Description = template.Description
  63. };
  64. return aipTemplate;
  65. }
  66. }
  67. }