using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Blog3000.Server.Utils { public class DataProtect { private static DataProtect _static; public static DataProtect Static { get { if (_static == null) { var serviceCollection = new ServiceCollection(); serviceCollection.AddDataProtection(); var services = serviceCollection.BuildServiceProvider(); // create an instance of MyClass using the service provider _static = ActivatorUtilities.CreateInstance(services); } return _static; } } private readonly IDataProtector protector; private readonly IDataProtectionProvider provider; public DataProtect(IDataProtectionProvider provider) { this.provider = provider; this.protector = provider.CreateProtector(this.GetType().FullName + "$GLOBAL"); } public string Protect(string value) { return protector.Protect(value); } public string Unprotect(string pv) { var r = protector.Unprotect(pv); return r; } } }