• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Try NCache
  • Contact Us
Try NCache
Show / Hide Table of Contents

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

  • .NET
  • .NET
  • Install the following NuGet package in your .NET client application:
    • NCache: Alachisoft.NCache.SDK
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Exceptions
    • System.Configuration
  • Add System.Configuration assembly 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.

try
{
    string cacheName = "defaultCache";

    var cacheConnectionOptions = new ConnectionOptions();

    // If the user wants to use username/password credentials, they can set the Security property to use those credentials
    cacheConnectionOptions.Security = SecurityOptions.CreateWithCredentials("your-username", "your-password");

    ICache cache = CacheManager.GetCache(cacheName, cacheConnectionOptions);
}
catch (ConfigurationException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.SERVER_INFO_NOT_FOUND)
    {
        // Configure server endpoints in the application configuration or ConnectionOptions.Servers.
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
    // Argument exception occurs in case of empty string name
}
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().

  • .NET
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.

Application configuration containing username and password settings

  • .NET
// 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);

See Also

NCache Data Encryption
Configuring Security

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Try NCache
Pricing
NCache Playground

Deployments
  • Cloud
  • Docker
  • Kubernetes
  • On-Premises
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • SignalR Backplane
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Blogs
  • Foundations
  • Videos
  • Comparisons
  • Shows
  • Whitepapers
  • Articles
  • Tech Talks
  • Magazine Articles
  • Third-Party Articles
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Docs
  • Schedule a Demo
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Terms of Use
  •  
  • Privacy Policy
  •  
  • Cookie Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS