.net core mvc项目分布式环境下Cookie数据保护解决方案
作者:互联网
public class CustomDataProtectionProvider : IDataProtectionProvider { public CustomDataProtectionProvider() { } public IDataProtector CreateProtector(string purpose) { return new CustomDataProtector(this); } } public class CustomDataProtector : IDataProtector { private readonly IDataProtectionProvider dataProtectionProvider; public CustomDataProtector(IDataProtectionProvider dataProtectionProvider) { this.dataProtectionProvider = dataProtectionProvider; } public IDataProtector CreateProtector(string purpose) { return this.dataProtectionProvider.CreateProtector(purpose); }
public byte[] Protect(byte[] plaintext) { var result = EncryptHelper.AesEncrypt(plaintext); return result; } public byte[] Unprotect(byte[] protectedData) { var reslut = EncryptHelper.AesDecrypt(protectedData); return reslut; }
.AddCookie("Cookies", x => { x.Cookie.Name = "Cookies"; x.DataProtectionProvider = new CustomDataProtectionProvider(); })
不必引用外部第三方组件来实现,只针对cookie代替原有KeyRingBasedDataProtectionProvider采用自身数据保护,不影响其他数据保护功能
标签:dataProtectionProvider,core,return,mvc,数据保护,IDataProtector,byte,public 来源: https://www.cnblogs.com/cnft/p/15107217.html