Skip to content

Commit

Permalink
Log (info level) if OIDC state data formatter is enabled with in memo…
Browse files Browse the repository at this point in the history
…ry distributed cache
  • Loading branch information
AndersAbel committed May 6, 2024
1 parent 01196b3 commit 937cca0
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

using Duende.IdentityServer.Infrastructure;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Linq;
Expand All @@ -21,12 +24,26 @@ public ConfigureOpenIdConnectOptions(string[] schemes, IServiceProvider serviceP
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
}

private static bool warnedInMemory = false;

public void PostConfigure(string name, OpenIdConnectOptions options)
{
// no schemes means configure them all
if (_schemes.Length == 0 || _schemes.Contains(name))
{
options.StateDataFormat = new DistributedCacheStateDataFormatter(_serviceProvider, name);
}

var distributedCacheService = _serviceProvider.GetRequiredService<IDistributedCache>();

if (distributedCacheService is MemoryDistributedCache && !warnedInMemory)
{
var logger = _serviceProvider
.GetRequiredService<ILogger<ConfigureOpenIdConnectOptions>>();

logger.LogInformation("You have enabled the OidcStateDataFormatterCache but the distributed cache registered is the default memory based implementation. This will store any OIDC state in memory on the server that initiated the request. If the response is processed on another server it will fail. If you are running in production, you want to switch to a real distributed cache that is shared between all nodes.");

warnedInMemory = true;
}
}
}

0 comments on commit 937cca0

Please sign in to comment.