.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