IdentityServer4.Contrib.RedisStore 1.2.0-beta

This is a prerelease version of IdentityServer4.Contrib.RedisStore.
There is a newer version of this package available.
See the version list below for details.
dotnet add package IdentityServer4.Contrib.RedisStore --version 1.2.0-beta                
NuGet\Install-Package IdentityServer4.Contrib.RedisStore -Version 1.2.0-beta                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IdentityServer4.Contrib.RedisStore" Version="1.2.0-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IdentityServer4.Contrib.RedisStore --version 1.2.0-beta                
#r "nuget: IdentityServer4.Contrib.RedisStore, 1.2.0-beta"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IdentityServer4.Contrib.RedisStore as a Cake Addin
#addin nuget:?package=IdentityServer4.Contrib.RedisStore&version=1.2.0-beta&prerelease

// Install IdentityServer4.Contrib.RedisStore as a Cake Tool
#tool nuget:?package=IdentityServer4.Contrib.RedisStore&version=1.2.0-beta&prerelease                

IdentityServer4.Contrib.RedisStore

IdentityServer4.Contrib.RedisStore is a persistance layer using Redis DB for operational data of Identity Server 4. Specifically, this store provides implementation for IPersistedGrantStore and ICache<T>.

How to use

You need to install the nuget package

then you can inject the stores in the Identity Server 4 Configuration at startup:

public void ConfigureServices(IServiceCollection services)
        {
            const string connectionString = @"---redis store connection string---";
            
            services.AddMvc();

            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddOperationalStore(connectionString, db = 0)
                .AddRedisCaching(connectionString, db = 1);
        }

Or by passing ConfigurationOptions instance, which contains the configuration of Redis store:

public void ConfigureServices(IServiceCollection services)
        {
            var operationalStoreOptions = new ConfigurationOptions {  /* ... */ };
            var cacheOptions = new ConfigurationOptions {  /* ... */ };
            services.AddMvc();

            services.AddIdentityServer()
                .AddTemporarySigningCredential()
                .AddOperationalStore(operationalStoreOptions)
                .AddRedisCaching(cacheOptions);
        }

#####Note:

operational store and caching are not related, you can use them separately or combined, it's totally fine.

the solution approach

the solution was approached based on how the SQL Store storing the operational data, but the concept of Redis as a NoSQL db is totally different than relational db concepts, all the operational data stores implement the following IPersistedGrantStore interface:

    public interface IPersistedGrantStore
    {
        Task StoreAsync(PersistedGrant grant);

        Task<PersistedGrant> GetAsync(string key);

        Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId);

        Task RemoveAsync(string key);

        Task RemoveAllAsync(string subjectId, string clientId);

        Task RemoveAllAsync(string subjectId, string clientId, string type);
    }

with the IPersistedGrantStore contract, we notice that the GetAllAsync(subjectId), RemoveAllAsync(subjectId,clientId) and RemoveAllAsync(subjectId,clientId,type) defines a contract to read based on subject id and remove all the grants in the store based on subject, client ids and type of the grant.

this brings trouble to Redis store since redis as a reliable dictionary is not designed for relational queries, so the trick is to store multiple key entries for the same grant, and the keys can be reached using key, subject, client ids and type.

so the StoreAsync operation stores the following entries in Redis:

  1. Key → RedisStruct: stored as key string value pairs, used to retrieve the grant based on the key, if the grant exists or not expired.

  2. Key(SubjectId) → Key* : stored in a redis Set, used on the GetAllAsync, to retrieve all the grant related to a given subject id.

  3. Key(SubjectId:ClientId) → Key* : stored in a redis set, used to retrieve all the keys that are related to a subject and client ids, to remove them while calling RemoveAllAsync.

  4. Key(SubjectId:ClientId:type) → Key* : stored in a redis set, used to retrieve all the keys that are related to a subject, client ids and type of the grant, to remove them while calling RemoveAllAsync.

for more information on data structures used to store the grant please refer to Redis data types documentation

since Redis has a key Expiration feature based on a defined date time or time span, and to not implement a logic similar to SQL store implementation for cleaning up the store periodically from dangling grants, the store uses the key expiration of redis while storing based on the following criteria:

  1. for Key of the grant, the expiration is straight forward, it's set on the StringSet Redis operation as defined by identity server on the grant object.

  2. for Key(SubjectId), Key(SubjectId:ClientId) and Key(SubjectId:clientId:type) the expiration is not set, since the same and only store type is persisting the grants regardless of their type, not like the identity server 3, where it has multiple store for each grant type.

Feedback

feedbacks are always welcomed, please open an issue for any problem or bug found, and the suggestions are also welcomed.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.5 is compatible.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on IdentityServer4.Contrib.RedisStore:

Package Downloads
LDFCore.Authorized

Package Description

LDFCore.Net5.Authorized

Package Description

BizzPo.IdentityServer

Package Description

MySoft.ServiceCenter.Core.IdentityServer

用于微服务器框架的核心库

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on IdentityServer4.Contrib.RedisStore:

Repository Stars
geffzhang/NanoFabric
基于Consul + .NET Core + Polly + Ocelot + Exceptionless + IdentityServer等开源项目的微服务开发框架
Version Downloads Last updated
4.0.0 484,845 7/3/2020 4.0.0 is deprecated because it is no longer maintained.
4.0.0-RC 1,169 6/25/2020
3.1.2 59,592 6/25/2020
3.1.1 45,258 2/20/2020
3.1.0 1,620 2/12/2020
3.0.2 30,086 10/12/2019
3.0.0 16,677 9/24/2019
3.0.0-preview2 412 9/9/2019
3.0.0-preview1 315 8/24/2019
2.1.2 92,614 10/12/2019
2.1.0 26,323 6/6/2019
2.0.0 47,735 11/22/2018
2.0.0-preview1 664 10/30/2018
1.4.5 16,391 10/2/2018
1.4.4.1 1,616 9/10/2018
1.4.4 1,129 9/8/2018
1.4.3 996 9/8/2018
1.4.2 7,419 6/18/2018
1.4.1 1,722 6/2/2018
1.4.1-rc1 829 6/1/2018
1.3.0 6,179 4/12/2018
1.2.0 2,571 2/28/2018
1.2.0-beta5 2,693 2/12/2018
1.2.0-beta2 898 2/8/2018
1.2.0-beta 777 2/7/2018
1.0.1 3,338 12/21/2017
1.0.0 1,583 9/5/2017
0.9.8-preview 912 8/29/2017
0.9.7-preview 949 8/28/2017
0.9.6-preview 916 8/24/2017

Added implementation for ICache interface, to support caching of clients, resources and CORS policies.