Redis connect with SSL/TLS using C# .NET
Today in this article we shall see how to enable Redis connect with SSL certificate using C# .NET application.
Install StackExchange.Redis
Please install the Redis NuGet package Microsoft.Extensions.Caching.StackExchangeRedis
as below,
PM>Install-Package Microsoft.Extensions.Caching.StackExchangeRedis -Version <version>
Or using .NET CLI
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis --version 5.0.1
Console application with Redis SSL
var configurationOptions = new ConfigurationOptions { EndPoints = { "localhost:6379" }, Ssl = true }; configurationOptions.CertificateSelection += delegate { var cert = new X509Certificate2(PATH_TO_CERT_FILE, ""); return cert; }; var redis = await ConnectionMultiplexer.ConnectAsync(configurationOptions); Console.WriteLine(redis.GetDatabase().Ping());
- ConfigurationOptions let’s you specify SSL property using the property “Ssl”. Please set this as true.
- PATH_TO_CERT_FILE is the location of the X509Certificate2 certificate file on the server.
Example– “c:\\cert\\path\\CERT.pfx”;
If using the ASP.NET Core application below is a sample that can be used to specify the certificate file.
If using the cloud with docker, please specify the docker file with the certificate location.
ASP.NET Core example for Redis SSL
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddScoped<ISalaryRepository, SalaryRepository>(); services.AddDbContext<EmployeeContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("EmployeeDB"), sqlServerOptionsAction: sqlOptions => { sqlOptions.EnableRetryOnFailure(); }); }); if (_hostEnvironment.IsDevelopment()) { services.AddDistributedMemoryCache();//Use this for only DEV testing purpose } else { var configurationOptions = new ConfigurationOptions { EndPoints = { $"{Configuration.GetValue<string>("RedisCache:Host")}:{Configuration.GetValue<int>("RedisCache:Port")}" }, Ssl = true, }; configurationOptions.CertificateSelection += delegate { var cert = new X509Certificate2(Configuration.GetValue<string>("RedisCache:CertificatePath"), ""); return cert; }; var multiplexer = ConnectionMultiplexer.Connect(configurationOptions); ; services.AddSingleton<IConnectionMultiplexer>(multiplexer); }
In a case using a self-signed certificate, then please make sure to install the certificate on your server.
References:
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
I was able to ssl the Redis server I had started on a VM with the following codes
try
{
ConfigurationOptions configurationOptions = new ConfigurationOptions
{
KeepAlive = 0,
AllowAdmin = true,
EndPoints = { { “SERVER IP ADDRESS”, 6379 }, { “127.0.0.1”, 6379 } },
ConnectTimeout = 5000,
ConnectRetry = 5,
SyncTimeout = 5000,
AbortOnConnectFail = false,
};
configurationOptions.CertificateSelection += delegate
{
var cert = new X509Certificate2(“PFX FILE PATH”, “”);
return cert;
};
ConnectionMultiplexer connection =
ConnectionMultiplexer.Connect(configurationOptions);
IDatabase databaseCache = connection.GetDatabase();
//set value
databaseCache.StringSet(“KEYNAME”, “KEYVALUE”);
//get Value
label_show_value.Text = databaseCache.StringGet(“KEYNAME”).ToString();
}
catch (Exception e1)
{
}
Hi Sadegh- Thanks for sharing! Glad you find the article helpful.