.net core MVC 集群DataProtection


.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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM