Introduction
Identity Server4 is an open-source authentication provider with OpenID connect and OAuth2.0 framework for ASP.NET Core. It acts as a centralized authentication provider or security token server (STS). It will be ideal to go through layers when you have multiple API/microservices applications and you should have single security token server to handle the authentication so that you really don’t have to define the Authentication in each and every application.
Identity server4 is a simple and straightforward STS. The user uses the clients (ASP.NET MVC or angular or react application and so on) to access the data, these users are authenticated by Identity Server to use the client. After successful authentication, the Identity server will send a token to client. Then client should use this token to access the data from the APIs.
Identity server4 provides the flexibility to use external storage like relational database, NoSQL, or in-memory data storage. NCache, being a distributed and scalable in-memory key-value data store is a great fit for IdentityServer4. In this article, you will learn how to use NCache as external in-memory storage for identityServer4. The main advantage of using external in-memory storage is to improve the application performance
Advantages of using Identity Server4
- Secure your resources
- Authenticate users either using a local account store or an external provider
- Validate tokens
What is NCache?
NCache is an open source distributed In-memory datastore, also called as distributed cache, in the .NET space and In-memory data gird in the java space. Since it is a distributed In-memory it’s extremely fast and scalable. It’s a native .NET solution and it’s easy to integrate to our application with .NET Stack. We will get a lot of benefits from NCache with our application, while dealing with processing the large amount of transaction during peak times. Basically, data storage and database cannot cope with such high transactions loads and becomes a bottleneck. To overcome this performance issue, we can go with Distributed cache with NCache which can store all transient or temporary data in it. Unlike database, NCache distributed cache allows you to build a caching tier, of two or more servers, pools the memory and CPU of all the cache server into one logical capacity. NCache will also provide a provision to add more servers as your transaction loads grow. So, NCache can scale linearly.
Using NCache as IdentityServer4
NCache can be used with IdentityServer in the following ways,
- NCache as IdentityServer4 Store
- NCache as IdentityServer4 Cache
Note: Both cache and store roles of NCache with IdentityServer4 can also be a mix and match using the implementation from IIdentityServerBuilder NCache extension for ASP.NET Core.
For NCache Installation, I highly recommend you go through a step-by-step tutorial on how to download and install a NCache on your windows machine here.
1. NCache as an In-Memory IdentityServer4 Store
Configuration and operational data, such as client information, Identity resources, API resources, device flow codes, etc., can be stored using NCache as an In-Memory IdentityServer4 Store, making this a much better server alternative for storing data.
With its In-Memory caching, NCache as a store speeds up operational flow and eliminates database hits. Essentially, IdentityServer4 handles all the required authentication at each network hop in your application, and NCache works well to store the operational and configuration data for quicker access.
The below figure will give you a complete understanding of NCache as an In-Memory IdentityServer4 Store.

NCache as IdentityServer4 Storage
From the above figure its obvious the NCache an in-memory distributed cache can acts as both a Configuration and Operational storage or Configuration storage as a SQL Server or some other data storage and the Operational storage as an NCache Server and vice versa.
What is Configuration storage?
It's a storage where the static data are stored, which doesn’t change frequently. In our case the data like client and identity resource information which remains unchanged for a long time will be stored.
What is an Operational storage?
It's a storage where the frequently modified data will be stored. Basically, it holds the sensitive data with up-to-date information integrated from operational sources. In our case it can be persisted grants and device flow codes.
Configure NCache for Configuration and Operational data store
For a demo, I have used my existing ASP.NET Core IdentityServer4 application, you can download it from GiHub.
- Download and install “Alachisoft.NCache.IdentityServer4” package from NuGet Package Manager
- Create a new Startup file “StartupNCache.cs” and add the following code.
public void ConfigureServices(IServiceCollection services) {
services.AddControllersWithViews();
var builder = services.AddIdentityServer().AddTestUsers(TestUsers.Users)
/// Add NCache as a configuration store
.AddNCacheConfigurationStore(options => {
options.CacheId = _configuration["NCacheConfiguration:CacheId"];
var serverList = _configuration["Servers"].Split(',').Select(x => x.Trim()).ToList().Select(y => new NCacheServerInfo(y, 9800)).ToList();
options.ConnectionOptions = new NCacheConnectionOptions {
ServerList = serverList,
EnableClientLogs = true,
LogLevel = NCacheLogLevel.Debug
};
})
/// Add NCache as an operational store
.AddNCachePersistedGrantStore(options => {
options.CacheId = _configuration["NCacheConfiguration:CacheId"];
var serverList = _configuration["NCacheConfiguration:Servers"].Split(',').Select(x => x.Trim()).ToList().Select(y => new NCacheServerInfo(y, 9800)).ToList();
options.ConnectionOptions = new NCacheConnectionOptions {
ServerList = serverList,
EnableClientLogs = true,
LogLevel = NCacheLogLevel.Debug
};
}).AddNCacheDeviceCodeStore(options => {
options.CacheId = _configuration["NCacheConfiguration:CacheId"];
var serverList = _configuration["NCacheConfiguration:Servers"].Split(',').Select(x => x.Trim()).ToList().Select(y => new NCacheServerInfo(y, 9800)).ToList();
options.ConnectionOptions = new NCacheConnectionOptions {
ServerList = serverList,
EnableClientLogs = true,
LogLevel = NCacheLogLevel.Debug
};
})
}








Join the conversation! Your thoughts help the community grow.