NCache 4.4 - Online Documentation

Using Security

 
For using security after configuring security, security credentials need to be mentioned for initializing cache.
 
try
{
Cache cache = NCache.InitializeCache("mycache", new Alachisoft.NCache.Web.Security.SecurityParams("Admin", "password"), null);
}
catch (Exception ex)
{
// handle exception
}
Or CacheInitParam can be used to set security credentials as follows:
try
{
    CacheInitParams ciParam = new CacheInitParams();
    ciParam.PrimaryUserCredentials = new SecurityParams("primaryUserId", "primaryUserPassword");
    ciParam.SecondaryUserCredentials = new SecurityParams("secondaryUserId", "secondaryUserPassword");
                
    cache = Alachisoft.NCache.Web.Caching.NCache.InitializeCache("cacheId", "server", port, ciParam.PrimaryUserCredentials, ciParam.SecondaryUserCredentials);
               
}
catch (Exception exp)
{
    // handle exception
}
 
Or  security credentials can be mentioned in app.config file of the application.
 
try
{
    string userName = String.Empty;
    string password = String.Empty;
    if(System.Configuration.ConfigurationManager.AppSettings["Username "]!=null)
    {
    userName=System.Configuration.ConfigurationManager.AppSettings["Username"].ToString();
    }
    if(System.Configuration.ConfigurationManager.AppSettings["Password"]!=null)
    {
    password=System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
    }
// Initializing cache with security credential provided in web/app .config file
    Cache cache = NCache.InitializeCache("mycache", new  Alachisoft.NCache.Web.Security.SecurityParams(userName, password), null);           
 
}
catch (Exception ex)
{
    // handle exception
}
 
 
See Also