简体   繁体   中英

ASP.Net Core web-farm with distributed session handling in Redis

I'm trying to create a web-farm with multiple docker containers all running the same ASP.NET Core 3.1 app. Hence, I need to manage a distributed session and my idea was to use Redis to do that.

According to this article I added the following statements to my ConfigureServices method:

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";
});

Of course, I added two packages from Nuget to my app:

  • Microsoft.AspNetCore.DataProtection.StackExchangeRedis
  • Microsoft.Extensions.Caching.StackExchangeRedis

Unfortunately, distributed session handling does not work and I get a bunch of errors like the following:

System.Security.Cryptography.CryptographicException: The key {9b51134a-a57a-4129-9441-72859a985ab0} was not found in the key ring.

at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.DangerousUnprotect(Byte[] protectedData, Boolean ignoreRevocationErrors, Boolean& requiresMigration, Boolean& wasRevoked)
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.Unprotect(Byte[] protectedData)
at Microsoft.AspNetCore.Session.CookieProtection.Unprotect(IDataProtector protector, String protectedText, ILogger logger)

Microsoft.AspNetCore.Session.SessionMiddleware: Warning: Error unprotecting the session cookie.

In Redis the key "DataProtection-Keys" was created but has not value.

How can I successfully configure distributed session/key management with Redis?

Best regards...

I actually found an answer by myself. I had to delete all existing localhost cookies from my browser. After that, everything worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM