Store DataProtection keys in Redis to fix cross-pod cookie decryption

Without shared key storage, pod A's cookies were unreadable by pod B, causing
an infinite OIDC redirect loop that appeared as access denied. Keys are now
persisted under SOR.Admin:DataProtection-Keys in the existing Redis instance.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-04-04 13:34:44 -04:00
co-authored by Claude Happy
parent f9ec834a7a
commit e4ddd801e3
3 changed files with 18 additions and 7 deletions
+15 -7
View File
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Identity;
@@ -141,13 +142,20 @@ builder.Services.AddIdentityCore<Account>(options =>
builder.Services.AddRazorPages();
// ── Redis ──────────────────────────────────────────────────────────────────
builder.Services.AddSingleton<IConnectionMultiplexer>(_ =>
{
var redisConnectionString = builder.Configuration.GetConnectionString("Redis") ?? "localhost:6379";
var redisOptions = ConfigurationOptions.Parse(redisConnectionString);
redisOptions.AbortOnConnectFail = false;
return ConnectionMultiplexer.Connect(redisOptions);
});
// Create the connection eagerly so it can be shared with DataProtection below.
var redisConnectionString = builder.Configuration.GetConnectionString("Redis") ?? "localhost:6379";
var redisConfig = ConfigurationOptions.Parse(redisConnectionString);
redisConfig.AbortOnConnectFail = false;
var redis = ConnectionMultiplexer.Connect(redisConfig);
builder.Services.AddSingleton<IConnectionMultiplexer>(redis);
// ── DataProtection (Redis-backed) ─────────────────────────────────────────
// Persist DataProtection keys in Redis so all pod replicas share the same key
// ring. Without this, cookies set by pod A cannot be decrypted by pod B,
// causing a redirect loop: OIDC flow completes → cookie set → redirect →
// different pod → cookie unreadable → user appears anonymous → loop.
builder.Services.AddDataProtection()
.PersistKeysToStackExchangeRedis(redis, "SOR.Admin:DataProtection-Keys");
// ── CDN client ────────────────────────────────────────────────────────────
builder.Services.AddCdnClient(builder.Configuration);
+1
View File
@@ -15,6 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" />
<PackageReference Include="StackExchange.Redis" />
<ProjectReference Include="..\SOR.Cdn\SOR.Cdn.csproj" />