NCache as ITicketStore Provider
The NCache ITicketStore integration provides a distributed backing store for ASP.NET Core Cookie Authentication through the ITicketStore interface. It allows ASP.NET Core applications to store complex user identity structures (authentication tickets) in NCache so that authenticated sessions can be shared across multiple application instances in a load-balanced environment.
NCache ITicketStore integration can be configured using the following approaches:
Using appsettings.json (Recommended for Production)
Using an Action Delegate in Program.cs
Both approaches require a custom implementation of the ITicketStore interface to map ASP.NET Core authentication middleware lifecycle events directly to NCache client commands.
Note
This feature is supported only in the NCache OpenSource edition.
Important
ITicketStore is supported in NCache OSS 5.3.6.2 and later.
Prerequisites
Before configuring ASP.NET Core ITicketStore with NCache, ensure that the following prerequisites are fulfilled:
- To configure ASP.NET Core ITicketStore with NCache, install the following NuGet package in your .NET application:
- OpenSource: NCache.OSS.AspNetCore.Authentication.TicketStore
- To utilize the ITicketStore provider, include the following namespaces in your application in Program.cs:
NCache.OSS.AspNetCore.Authentication.TicketStoreMicrosoft.AspNetCore.AuthenticationMicrosoft.AspNetCore.Authentication.CookiesSystem.Security.Claims
- The cache must be running.
Method 1: Using appsettings.json (Recommended)
This approach stores TicketStore provider settings inside appsettings.json and binds them automatically through IConfiguration during application startup. It is useful for environments where configuration values may change between deployments because cache settings can be modified without changing application code. This is the recommended approach for production environments because configuration changes can be made without recompiling the application.
In this configuration approach, NCache connection settings are defined inside appsettings.json while your initialization code reads and binds the configuration section manually during service registration into the dependency injection container.
Step 1: Configure NCache Settings
The following configuration defines the NCache cache name and server connectivity information used by the TicketStore provider. Add the following configuration section in your appsettings.json file.
{
"NCacheTicketStore": {
"CacheName": "demoCache",
"ServerList": [
{
"Ip": "20.200.20.29",
"Port": 9800
}
]
}
}
Step 2: Register the Configuration Section
After defining the NCache settings in appsettings.json, register the custom NCache TicketStore provider by binding it to the NCacheTicketStore configuration section. When a user authenticates via the /login endpoint, their full identity context is safely externalized to the NCache distributed cluster while the client browser receives a minimal session cookie wrapper.
Add the following code in your Program.cs file.
builder.Services.AddNCacheTicketStore(
builder.Configuration.GetSection("NCacheTicketStore"));
Method 2: Using an Action Delegate in Program.cs
In this approach, the TicketStore provider configuration is defined directly inside Program.cs through an action delegate passed to AddNCacheTicketStore. The action delegate allows the cache name and server connectivity parameters to be configured programmatically during application initialization.
The following configuration registers the custom NCache TicketStore provider using an inline action delegate, defining the target cache instance and populating the server connection list directly within the application startup routines.
Add the following code in your Program.cs file.
builder.Services.AddNCacheTicketStore(options =>
{
options.CacheName = "demoCache";
options.ServerList.Add(new NCacheOptions.ServerConfig
{
Ip = "20.200.20.29",
Port = 9800
});
});
Configuration Properties
The following configuration properties are available in the NCacheOptions class.
Note
Properties marked with an asterisk (*) are required. All other properties are optional.
| Property | Description |
|---|---|
| CacheName* | Specifies the name of the NCache cache instance. The cache must already exist in the NCache cluster and is required for provider initialization. |
| ServerList | Specifies the list of NCache server nodes used for cache connectivity. Each ServerConfig entry contains an IP address and port number. IP addresses must be valid IPv4 or IPv6 addresses and port values must be between 1–65535. |