【发布时间】:2021-03-02 20:01:01
【问题描述】:
我正在尝试创建一个包含多个 docker 容器的网络场,这些容器都运行相同的 ASP.NET Core 3.1 应用程序。因此,我需要管理一个分布式会话,而我的想法是使用 Redis 来做到这一点。
根据this article,我在ConfigureServices方法中添加了以下语句:
var redis = ConnectionMultiplexer.Connect(redis_connection_string);
services.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys")
.SetApplicationName("seminarmanager");
services.AddStackExchangeRedisCache(o =>
{
o.Configuration = redis_connection_string;
o.InstanceName = "seminarmanager";
});
当然,我在我的应用中添加了来自 Nuget 的两个包:
Microsoft.AspNetCore.DataProtection.StackExchangeRedisMicrosoft.Extensions.Caching.StackExchangeRedis
不幸的是,分布式会话处理不起作用,我收到一堆错误,如下所示:
System.Security.Cryptography.CryptographicException:在密钥环中找不到密钥 {9b51134a-a57a-4129-9441-72859a985ab0}。
在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
在 Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
在 Microsoft.AspNetCore.Session.CookieProtection.Unprotect(IDataProtector 保护器,字符串 protectedText,ILogger 记录器)Microsoft.AspNetCore.Session.SessionMiddleware:警告:取消保护会话 cookie 时出错。
在 Redis 中,键“DataProtection-Keys”已创建但没有值。
如何使用 Redis 成功配置分布式会话/密钥管理?
最好的问候...
【问题讨论】:
标签: c# asp.net-core session redis