.net core 分布式部署時,不使用Session,使用Redis+Cookie存儲數據,
但是會涉及到DataProtection的使用;
解決方法使用DataProtection到持續化到Redis中;
Redis 不能使用Microsoft.Extensions.Caching.Redis 包,會有包沖突,使用 Microsoft.Extensions.Caching.StackExchangeRedis
然后使用
Microsoft.AspNetCore.DataProtection.StackExchangeRedis包
然后在 ConfigureServices 方法中進行如下配置
#region DataProtection
//設置應用程序唯一標識
var redis =StackExchange.Redis.ConnectionMultiplexer.Connect(Configuration["Redis:Server"]);
services.AddDataProtection().SetApplicationName("APPIdentityName")
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");
#endregion
#region Redis配置
services.AddSingleton<StackExchange.Redis.IConnectionMultiplexer>(StackExchange.Redis.ConnectionMultiplexer.Connect(Configuration["Redis:Server"]));
services.AddStackExchangeRedisCache(options => {
options.Configuration = Configuration["Redis:Server"]; //數據庫配置的Redis連接字符串
options.InstanceName = Configuration["Redis:InstanceName"];//Redis實例名稱
});
#endregion
