【问题标题】:ASP.Net Core web-farm with distributed session handling in RedisRedis 中具有分布式会话处理的 ASP.Net Core web-farm
【发布时间】: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.StackExchangeRedis
  • Microsoft.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


    【解决方案1】:

    其实我自己找到了答案。我不得不从我的浏览器中删除所有现有的 localhost cookie。之后,一切正常。

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 2016-06-04
      • 2020-01-06
      • 2017-07-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 2011-08-30
      • 2015-02-12
      相关资源
      最近更新 更多