发布:2023/12/7 15:50:47作者:大数据 来源:大数据 浏览次数:621
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
//session services.AddDistributedSqlServerCache(options => { options.ConnectionString = Configuration.GetConnectionString("BoxDbcontext"); options.SchemaName = "dbo"; options.TableName = "global_cache"; }); var x = Configuration.GetConnectionString("BoxDbcontext"); services.AddDbContext<DataProtectionDbContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("BoxDbcontext"))); // Register XmlRepository for data protection. services.AddOptions<KeyManagementOptions>() .Configure<IServiceScopeFactory>((options, factory) => { options.XmlRepository = new CustomXmlRepository(factory); }); //redis存储 //var redis = ConnectionMultiplexer.Connect("localhost:6379"); //services.AddDataProtection().ProtectKeysWithDpapi().PersistKeysToRedis(redis, "DataProtection-Keys"); //文件存储 //将秘钥存储在本地,只能用于单台服务器上部署多个站点,站点之间的cookie数据共享 //services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"E:\xml")); //存储在UNC(Universal Naming Convention)网络共享服务器目录,可以用于多个服务器共享 //services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\xml\"); //存储在程序里ok //services.Configure<KeyManagementOptions>(options => //{ // options.XmlRepository = new MyCustomXmlRepository(); //}); //===== //services.AddSingleton<IXmlRepository, MyCustomXmlRepository>(); //var sp = services.BuildServiceProvider(); //services.AddDataProtection().AddKeyManagementOptions(o => o.XmlRepository = sp.GetService<IXmlRepository>()).SetApplicationName("kszx"); //===ok //services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(services => //{ // return new ConfigureOptions<KeyManagementOptions>(options => // { // options.XmlRepository = new MyCustomXmlRepository(); // }); //}); //==ok //services.AddOptions<KeyManagementOptions>().Configure<IServiceScopeFactory>((options, factory) => //{ // options.XmlRepository = new MyCustomXmlRepository(); //}); //// 在ConfigureServices中配置数据保护 //services.AddDataProtection(x => x.ApplicationDiscriminator = "session_application_name"); ////或者用这样的写法,效果是一样的 //services.AddDataProtection().SetApplicationName("session_application_name"); |
1 2 3 4 5 6 7 8 9 10 |
public class XmlKey { public string Id { get; set; } public string Xml { get; set; } public XmlKey() { this.Id = Guid.NewGuid().ToString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public class CustomXmlRepository : IXmlRepository { private readonly IServiceScopeFactory factory; public CustomXmlRepository(IServiceScopeFactory factory) { this.factory = factory; } public IReadOnlyCollection<XElement> GetAllElements() { using (var scope = factory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService<DataProtectionDbContext>(); var keys1 = context.XmlKey.ToList(); var keys = context.XmlKey.ToList() .Select(x => XElement.Parse(x.Xml)) .ToList(); return keys; } } public void StoreElement(XElement element, string friendlyName) { var key = new XmlKey { Xml = element.ToString(SaveOptions.DisableFormatting) }; using (var scope = factory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService<DataProtectionDbContext>(); context.XmlKey.Add(key); context.SaveChanges(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
public class MyCustomXmlRepository : IXmlRepository { private readonly string keyContent = @"<?xml version='1.0' encoding='utf-8'?> <key id='659113f1-ad55-40b5-9a36-69876284756d' version='1'> <creationDate>2022-08-03T09:03:26.3997789Z</creationDate> <activationDate>2022-08-03T09:03:26.365547Z</activationDate> <expirationDate>2022-11-01T09:03:26.365547Z</expirationDate> <descriptor deserializerType='Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'> <descriptor> <encryption algorithm='AES_256_CBC' /> <validation algorithm='HMACSHA256' /> <masterKey p4:requiresEncryption='true' xmlns:p4='http://schemas.asp.net/2015/03/dataProtection'> <!-- Warning: the key below is in an unencrypted form. --> <value>dkrtFmq5vnjCs3qTdr0LrzbZBYmloVrShWBSXAmIH1fUuPWm0XEKX5Vi49B4DwVIMhCoovh13RV0rm6vC5WmmA==</value> </masterKey> </descriptor> </descriptor> </key>"; //public IReadOnlyCollection<XElement> GetAllElements() //{ // throw new System.NotImplementedException(); //} //public void StoreElement(XElement element, string friendlyName) //{ // throw new System.NotImplementedException(); //} public virtual IReadOnlyCollection<XElement> GetAllElements() { return GetAllElementsCore().ToList().AsReadOnly(); } private IEnumerable<XElement> GetAllElementsCore() { yield return XElement.Parse(keyContent); } public virtual void StoreElement(XElement element, string friendlyName) { if (element == null) { throw new ArgumentNullException(nameof(element)); } StoreElementCore(element, friendlyName); } private void StoreElementCore(XElement element, string filename) { // element.Save(@"e:\xml\" + Guid.NewGuid() + ".xml"); } } |
1 2 3 4 5 6 7 |
CREATE TABLE [dbo].[global_cache]( [Id] [nvarchar](500) NOT NULL, [Value] [varbinary](max) NOT NULL, [ExpiresAtTime] [datetimeoffset](7) NOT NULL, [SlidingExpirationInSeconds] [bigint] NULL, [AbsoluteExpiration] [datetimeoffset](7) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] |
1 2 3 4 |
CREATE TABLE [dbo].[XmlKey]( [Id] [nvarchar](max) NULL, [Xml] [nvarchar](max) NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] |
解决分布式集群登录cookies和验证码过期不一致问题
数据保护
参考文档:
https://docs.microsoft.com/zh-cn/aspnet/core/performance/caching/distributed?view=aspnetcore-6.0
https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/web-farm?view=aspnetcore-6.0
https://docs.microsoft.com/zh-cn/aspnet/core/security/cookie-sharing?view=aspnetcore-6.0
https://docs.microsoft.com/zh-cn/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-6.0
https://www.icode9.com/content-1-851846.html
https://www.cnblogs.com/ericli-ericli/articles/6826756.html
https://tieba.baidu.com/p/7434463479
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4