How to use Security?
NCache provides two ways to secure your cache and prevent unwanted or unauthorized access to your NCache cluster. First, you can configure Client Node Security (and its respective cache security) using the NCache Management Center, allowing the clients connected with this node to access the cache with the same credentials you provided while configuring Client Node Security. No code change is required to do so.
Note
- This feature is only available in NCache Enterprise.
- This feature is provided for both Windows and Linux .NET applications.
Alternatively, you can configure it programmatically when the NCache client is not installed on your system, as demonstrated below.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- Add
System.Configuration
assembly through the Reference Manager. - For API details, refer to: ICache, CacheConnectionOptions, GetCache, CacheManager, Credentials.
Here's an example which shows how to enable security on the cache. Once security is configured on the cache, security credentials need to be mentioned for initializing cache, using CacheConnectionOptions
.
string cacheName = "demoCache";
// Use CacheConnectionOptions to provide credentials
var options = new CacheConnectionOptions();
// Use UserCredentials property to assign credentials
options.UserCredentials = new Credentials("your-username", "your-password");
// Connecting to cache with security credentials
ICache cache = CacheManager.GetCache(cacheName, options);
Or as an alternative, the security credentials can be provided in the property file of your application:
// Initialize credentials
string userName = string.Empty;
string password = string.Empty;
string cacheName = "demoCache";
// Check if Username and Password is 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 CacheConnectionOptions to provide credentials
var options = new CacheConnectionOptions();
options.UserCredentials = new Credentials(userName, password);
// Connect to cache with security credentials provided in App.config file
ICache cache = CacheManager.GetCache(cacheName, options);
Troubleshooting
Alachisoft.NCache.Runtime.Exceptions.SecurityException
This exception is raised if an unauthorized user tries to perform cache operations, or wrong credential information is given in the GetCache()
or Configure Security in client.ncconf file.
Workaround
Check if you have given correct credential information through API or in client.ncconf. A typing mistake can be the result of this exception.
See if the specified user exists under a given domain in LDAP. The login credentials are required to belong to any server hosting the user login and running LDAP services.
See Also
NCache Data Encryption
Stream Processing in Cache
Configuring Security