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:
@@ -25,6 +25,8 @@
|
||||
<!-- ── Redis ─────────────────────────────────────────────────────────── -->
|
||||
<!-- StackExchange.Redis: battle-tested, high-performance Redis client for .NET -->
|
||||
<PackageVersion Include="StackExchange.Redis" Version="2.7.33" />
|
||||
<!-- DataProtection keys stored in Redis so all pods share the same key ring -->
|
||||
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="8.0.0" />
|
||||
<!-- ── Entity Framework Core ─────────────────────────────────────────── -->
|
||||
<!-- EF Core ORM: maps C# classes to database tables, generates SQL queries -->
|
||||
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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" />
|
||||
|
||||
Reference in New Issue
Block a user