123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using Aip.Service.Aip.Models;
- using System.Security.Cryptography.X509Certificates;
- using Microsoft.InformationProtection;
- using Microsoft.InformationProtection.Protection;
- namespace Aip.Service.Aip;
- public static class AipUtils
- {
- 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 is null)
- {
- value = string.Empty;
- }
- if (!value.EndsWith("/", StringComparison.Ordinal))
- {
- return value + "/";
- }
- return value;
- }
- public static AipLabel? LabelToAip(Label label)
- {
- if (label is null) return null;
- return new AipLabel
- {
- Id = label.Id,
- Name = label.Name,
- Sensitivity = label.Sensitivity,
- Description = label.Description,
- IsActive = label.IsActive,
- ActionSource = (AipActionSource)label.ActionSource,
- Children = new List<AipLabel>()
- };
- }
- public static AipTemplate? TemplateToAip(TemplateDescriptor template)
- {
- if (template is null) return null;
- var aipTemplate = new AipTemplate
- {
- Id = template.Id,
- Name = template.Name,
- Description = template.Description
- };
- return aipTemplate;
- }
- }
|