How to use Security?
Configure Cache Node Security through the NCache Management Center to restrict access to the cache. Client applications must then authenticate using an authorized identity. This page explains how to supply username and password credentials or a gMSA identity through the .NET connection API.
Additionally, NCache enables cache users to securely connect to the cache using a Group Managed Service Account (gMSA) identity using a Security property of type SecurityOptions.
Prerequisites
- Install the following NuGet package in your .NET client application:
- NCache:
Alachisoft.NCache.SDK
- NCache:
- Include the following namespaces in your application:
- Alachisoft.NCache.Client
- Alachisoft.NCache.Runtime.Exceptions
System.Configuration
- Add
System.Configurationassembly through Reference Manager. - Cache must be running.
- To learn about gMSA requirements, please see Configure gMSA section.
- For API details, refer to: ICache, ConnectionOptions, GetCache, CacheManager, UserCredentials, CreateWithCredentials, CreateWithGMSA, SecurityOptions.
Using Security API
The Security property of the ConnectionOptions class allows you to either provide a username and password or use gMSA for password-less authentication. You can use helper methods like CreateWithCredentials or CreateWithGMSA to create the desired configuration, which is used during cache connection.
Connect using CreateWithCredentials API
The following example demonstrates how to use the SecurityOptions API to provide a username and password when initializing the cache.
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Connect using gMSA Security
The following code sample shows how to configure the cache connection to use Group Managed Service Account (gMSA) credentials for password-less authentication, using CreateWithGMSA().
string cacheName = "defaultCache";
var cacheConnectionOptions = new ConnectionOptions();
cacheConnectionOptions.Security = SecurityOptions.CreateWithGMSA();
ICache cache = CacheManager.GetCache(cacheName, cacheConnectionOptions);
Connect via App.config using Credentials
The following code sample demonstrates how to retrieve security credentials stored in the App.config file and apply them using the SecurityOptions property.

// Initialize credentials
string userName = string.Empty;
string password = string.Empty;
string cacheName = "defaultCache";
// Check if Username and Password are provided through App.config file
if (ConfigurationManager.AppSettings["Username"] != null)
{
userName = ConfigurationManager.AppSettings["Username"].ToString();
}
if (ConfigurationManager.AppSettings["Password"] != null)
{
password = ConfigurationManager.AppSettings["Password"].ToString();
}
// Use ConnectionOptions to provide credentials
var cacheConnectionOptions = new ConnectionOptions();
cacheConnectionOptions.Security = SecurityOptions.CreateWithCredentials(userName, password);
// Connect to cache with security credentials provided in App.config file
ICache cache = CacheManager.GetCache(cacheName, cacheConnectionOptions);