NCache as an In-Memory IdentityServer4 Store
NCache can be be used as a caching layer on top of the IdentityServer4 persistent configuration and operational data stores so that operations occurring can speed up through in-memory caching as well as reduce database hits.
Using NCache as an IdentityServer4 store means storing information about the clients, API and identity resources etc. You are given an option of using NCache as one store or with both of them. Now let's see through the following diagram how NCache acts as an IdentitySever4 store:
Pre-Requisites
- Use .NET Core 3.1 SDK and runtimes for IdentityServer4.
- NCache Enterprise 5.0 SP1 or onwards should be running on your server(s).
- The cache name and IP address information has been changed according to your application.
- Cache must be running.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
In order to use NCache for the IdentityServer4 configuration and operational data, configurations need to be done as explained in detail below.
How to Configure NCache as IdentityServer4 Store
Step 1:
- Install the following NuGet (Version 5.2) package to your application:
Alachisoft.NCache.IdentityServer4
- Include the following namespaces in the StartupNCache.cs file of your application.
IdentityServer4.NCache.Entities
IdentityServer4.NCache.Mappers
IdentityServer4.NCache.Options
IdentityServer4.NCache.Stores.Interfaces
Step 2: In the cs of your project, add the .UseStartup<StartupNCache>()
method. Then for NCache as both a configuration and operational store, add the following code where CacheId
is the name of your cache and Servers
are the IP addresses of your nodes in the StartupNCache.cs file.
Warning
If the client.ncconf file in the project folder or your installation directory i.e. %NCHOME%/config
do not contain in the information regarding the cache cluster, then the ConnectionOptions
must be at least set with the information of at least one of the servers making up the NCache cluster. That information is provided within the ServerList
property of the NCacheConnectionOptions
class.
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["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["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
};
})
.AddNCacheDeviceCodeStore(options =>
{
options.CacheId = _configuration["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
};
})
///..rest of the code
}
Step 3: After that, in the appsettings.json file, modify the value of the CacheId
key to the name of the cache you are using. Furthermore, for the multiple Servers keys, use a comma separated list of one or more IP addresses belonging to the NCache servers making up the NCache cluster.
{
"CacheId" : "demoCache",
"Servers" : "20.200.20.48,2.200.20.35",
}
Step 4: Run your applications IdentityServer
, MvcClient
, Api
, JavaScriptClient
to see how NCache operates as an IdentityServer4 configuration and operational store. Make sure that the demonstration cache used as both a configuration and operational store is running and can be connected to the IdentityServer
sample application.
Additional Resources
NCache provides sample application for IdentityServer4 at GitHub.
See Also
NCache as an in-memory IdentityServer4 Cache Implementation
Cache Keys and Data Overview