AipUtils.cs 2.3 KB

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