diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml
index 13cba00..2446fe1 100644
--- a/.github/workflows/pull-request.yml
+++ b/.github/workflows/pull-request.yml
@@ -39,4 +39,5 @@ jobs:
- name: Check vulnerable packages
run: |
dotnet list package --vulnerable --include-transitive 2>&1 | tee vulnerable.txt
- ! grep -q "has the following vulnerable packages" vulnerable.txt
+ # IdentityServer4 is EOL with known advisories and is accepted on purpose (docs/Decisions/ADR-002)
+ ! grep -E '^\s+>' vulnerable.txt | grep -v ' IdentityServer4 '
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 683ed0f..a99f97e 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -30,59 +30,62 @@
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/docs/Architecture/Stores.md b/docs/Architecture/Stores.md
index 03b166f..475bb63 100644
--- a/docs/Architecture/Stores.md
+++ b/docs/Architecture/Stores.md
@@ -1,6 +1,6 @@
---
tags: [architecture, storage]
-updated: 2026-10-01
+updated: 2026-11-17
---
# Stores (`IJsonWebKeyStore`)
@@ -9,7 +9,7 @@ Contract (`Interfaces/IJsonWebKeyStore.cs`): `Store`, `GetCurrent(type)`, `Revok
| Store | Registration | Persistence | Notes |
| --- | --- | --- | --- |
| `DataProtectionStore` (default, internal) | `AddJwksManager()` | ASP.NET DataProtection `IXmlRepository` (same place as cookie keys) | Payload protected with `IDataProtector`. Revocation is a separate XML element because `IXmlRepository` can't update. If no repository can be found, it falls back to Azure WebSites → default folder → Windows registry, then throws |
-| `InMemoryStore` | `.PersistKeysInMemory()` | Process memory | Tests/dev only |
+| `InMemoryStore` | `.PersistKeysInMemory()` | Memory of the service provider (singleton) | Tests/dev only. Each service provider has its own keys: hosts in the same process (e.g. parallel tests) don't share them. Lost on restart |
| `FileSystemStore` | `.PersistKeysToFileSystem(dir)` | JSON files in a folder | Needs `IMemoryCache` |
| `DatabaseJsonWebKeyStore` | `.PersistKeysToDatabaseStore()` | EF Core, `TContext : DbContext, ISecurityKeyContext` (`DbSet SecurityKeys`) | Mapping in `KeyMaterialMap.cs`; uses `AsNoTrackingWithIdentityResolution` |
diff --git a/src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs b/src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs
index 9bd2511..989e0f8 100644
--- a/src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs
+++ b/src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs
@@ -5,50 +5,55 @@
namespace NetDevPack.Security.Jwt.Core.DefaultStore;
+///
+/// Keeps the keys in memory. Registered as a singleton, so each service provider (application)
+/// has its own keys instead of sharing a static list with every other host in the process.
+///
internal class InMemoryStore : IJsonWebKeyStore
{
- private static readonly List _store = new();
- private readonly SemaphoreSlim _slim = new(1);
+ private readonly List _store = new();
+ private readonly object _lock = new();
internal const string DefaultRevocationReason = "Revoked";
public Task Store(KeyMaterial keyMaterial)
{
if (keyMaterial is null) throw new InvalidOperationException("Can't store empty value.");
- _slim.Wait();
- _store.Add(keyMaterial);
- _slim.Release();
+ lock (_lock)
+ _store.Add(keyMaterial);
return Task.CompletedTask;
}
public Task GetCurrent(JwtKeyType jwtKeyType = JwtKeyType.Jws)
{
- return Task.FromResult(_store.Where(s => s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc")).OrderByDescending(s => s.CreationDate).FirstOrDefault());
+ var use = jwtKeyType == JwtKeyType.Jws ? "sig" : "enc";
+ lock (_lock)
+ return Task.FromResult(_store.Where(s => s.Use == use).OrderByDescending(s => s.CreationDate).FirstOrDefault());
}
- public async Task Revoke(KeyMaterial keyMaterial, string reason = null)
+ public Task Revoke(KeyMaterial keyMaterial, string reason = null)
{
- if(keyMaterial == null)
- return;
- var revokeReason = reason ?? DefaultRevocationReason;
- keyMaterial.Revoke(revokeReason);
- var oldOne = _store.Find(f => f.Id == keyMaterial.Id);
- if (oldOne != null)
+ if (keyMaterial == null)
+ return Task.CompletedTask;
+
+ keyMaterial.Revoke(reason ?? DefaultRevocationReason);
+ lock (_lock)
{
var index = _store.FindIndex(f => f.Id == keyMaterial.Id);
- await _slim.WaitAsync();
- _store.RemoveAt(index);
- _store.Insert(index, keyMaterial);
- _slim.Release();
+ if (index >= 0)
+ _store[index] = keyMaterial;
}
+
+ return Task.CompletedTask;
}
public Task> GetLastKeys(int quantity, JwtKeyType? jwtKeyType = null)
{
- return Task.FromResult(
- _store
- .Where(s => jwtKeyType == null || s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc"))
+ lock (_lock)
+ return Task.FromResult(
+ _store
+ .Where(s => jwtKeyType == null || s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc"))
.OrderByDescending(s => s.CreationDate)
.GroupBy(s => s.Use)
.SelectMany(g => g.Take(quantity))
@@ -57,12 +62,15 @@ public Task> GetLastKeys(int quantity, JwtKeyTyp
public Task Get(string keyId)
{
- return Task.FromResult(_store.FirstOrDefault(w => w.KeyId == keyId));
+ lock (_lock)
+ return Task.FromResult(_store.FirstOrDefault(w => w.KeyId == keyId));
}
public Task Clear()
{
- _store.Clear();
+ lock (_lock)
+ _store.Clear();
+
return Task.CompletedTask;
}
-}
\ No newline at end of file
+}
diff --git a/src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs b/src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs
index 0bd9012..d06c8a4 100644
--- a/src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs
+++ b/src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs
@@ -24,12 +24,13 @@ public static IJwksBuilder AddJwksManager(this IServiceCollection services, Acti
}
///
- /// Sets the signing credential.
+ /// Keeps the keys in memory, for the lifetime of the service provider. Keys are lost on restart
+ /// and are not shared between instances, so use it only for development and tests.
///
///
public static IJwksBuilder PersistKeysInMemory(this IJwksBuilder builder)
{
- builder.Services.AddScoped();
+ builder.Services.AddSingleton();
return builder;
}
diff --git a/tests/NetDevPack.Security.Jwt.Tests/StoreTests/InMemoryStoreIsolationTests.cs b/tests/NetDevPack.Security.Jwt.Tests/StoreTests/InMemoryStoreIsolationTests.cs
new file mode 100644
index 0000000..d4a9a2a
--- /dev/null
+++ b/tests/NetDevPack.Security.Jwt.Tests/StoreTests/InMemoryStoreIsolationTests.cs
@@ -0,0 +1,46 @@
+using System.Threading.Tasks;
+using AwesomeAssertions;
+using Microsoft.Extensions.DependencyInjection;
+using NetDevPack.Security.Jwt.Core.Interfaces;
+using Xunit;
+
+namespace NetDevPack.Security.Jwt.Tests.StoreTests;
+
+[Trait("Category", "InMemory Tests")]
+public class InMemoryStoreIsolationTests
+{
+ private static ServiceProvider CreateProvider()
+ {
+ var services = new ServiceCollection();
+ services.AddLogging();
+ services.AddMemoryCache();
+ services.AddJwksManager().PersistKeysInMemory();
+ return services.BuildServiceProvider();
+ }
+
+ [Fact]
+ public async Task ServiceProviders_ShouldNotShareKeys()
+ {
+ await using var first = CreateProvider();
+ await using var second = CreateProvider();
+
+ using (var scope = first.CreateScope())
+ await scope.ServiceProvider.GetRequiredService().GenerateKey();
+
+ using (var scope = second.CreateScope())
+ (await scope.ServiceProvider.GetRequiredService().GetLastKeys(5)).Should().BeEmpty();
+ }
+
+ [Fact]
+ public async Task Scopes_OfTheSameServiceProvider_ShouldShareKeys()
+ {
+ await using var provider = CreateProvider();
+
+ string keyId;
+ using (var scope = provider.CreateScope())
+ keyId = (await scope.ServiceProvider.GetRequiredService().GenerateKey()).KeyId;
+
+ using (var scope = provider.CreateScope())
+ (await scope.ServiceProvider.GetRequiredService().Get(keyId)).Should().NotBeNull();
+ }
+}