NCache 4.6 - Online Documentation

Using Security in NCache

 
For using security in NCache after configuring security, security credentials need to be mentioned for initializing cache using CacheInitParams:
 
      To utilize the API, include the following namespace in your application: Alachisoft.NCache.Web.Caching. 
 
            try
            {
                CacheInitParams ciParam = new CacheInitParams();
                ciParam.PrimaryUserCredentials = new SecurityParams("primaryUserId", "primaryUserPassword");
                ciParam.SecondaryUserCredentials = new SecurityParams("secondaryUserId", "secondaryUserPassword");
 
                Cache cache = NCache.InitializeCache("myreplicatedcache", ciParam);
            }
            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();
                }
 
                CacheInitParams cacheParam = new CacheInitParams();
                cacheParam.PrimaryUserCredentials = new SecurityParams(userName, password);
 
                // Initializing cache with security credential provided in web/app .config file
                Cache cache = NCache.InitializeCache("mycache", cacheParam);
            }
            catch (Exception ex)
            {
                // handle exception
            }
 
 
See Also