Path: redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Auth/UserTokenStore.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Web/Auth/UserTokenStore.cs ---- using System.Security.Cryptography; using System.Text.Json; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Caching.Distributed; namespace RedbIdentityApp.Web.Auth; /// /// The user's tokens, kept on this server: the session cookie carries only a random session key. A /// token never reaches the browser, and the cookie stays small whatever the tokens weigh. /// /// Storage is , encrypted with DataProtection. Program.cs registers the /// in-memory cache: one server, and a restart signs users out of this application (Identity's own /// session signs them back in without a password). For several replicas register a shared cache /// (Redis: AddStackExchangeRedisCache) and a shared DataProtection key ring. /// /// public sealed class UserTokenStore(IDistributedCache cache, IDataProtectionProvider protection) { /// The cookie claim that holds the session key. public const string SessionClaim = "redbidentityapp:session"; private readonly IDataProtector _protector = protection.CreateProtector("RedbIdentityApp.UserTokens"); /// How long a session outlives its last use; the cookie uses the same. public static readonly TimeSpan SessionLifetime = TimeSpan.FromHours(8); public async Task CreateAsync(UserTokens tokens, CancellationToken ct) { var key = Convert.ToHexString(RandomNumberGenerator.GetBytes(32)); await SaveAsync(key, tokens, ct); return key; } public async Task GetAsync(string key, CancellationToken ct) { var protectedBytes = await cache.GetAsync(CacheKey(key), ct); if (protectedBytes is null) return null; try { return JsonSerializer.Deserialize(_protector.Unprotect(protectedBytes)); } catch (CryptographicException) { // Keys changed (a new DataProtection ring): the entry is unreadable, the session is over. await RemoveAsync(key, ct); return null; } } public Task SaveAsync(string key, UserTokens tokens, CancellationToken ct) => cache.SetAsync(CacheKey(key), _protector.Protect(JsonSerializer.SerializeToUtf8Bytes(tokens)), new DistributedCacheEntryOptions { SlidingExpiration = SessionLifetime }, ct); public Task RemoveAsync(string key, CancellationToken ct) => cache.RemoveAsync(CacheKey(key), ct); private static string CacheKey(string key) => "redbidentityapp:tokens:" + key; } /// What the token endpoint gave the user's session. public sealed record UserTokens( string AccessToken, string? RefreshToken, string? IdToken, DateTimeOffset ExpiresAt, string[] Scopes);