Cannot connect to redis server with TLS from .NET Core

877 Views Asked by At

What I am doing

I have two Google Cloud instances.

  • A Cloud Run Service (.NET Core Web API Server)
  • Memorystore for Redis (TLS enabled)

I would like to connect to Memorystore Redis server from my .NET app running on Cloud Run.

My Code

I downloaded server-ca.pem from Google Cloud console, and I followed this sample code.

using StackExchange.Redis;
using System.Security.Cryptography.X509Certificates;

var redisOptions = new ConfigurationOptions
{
    EndPoints = {{"xx.xx.xx.xx", 6378}},
    Ssl = true,
    SslProtocols = SslProtocols.Tls12,
};

redisOptions.CertificateSelection += delegate
{
    var cert = new X509Certificate2("server-ca.pem", "");
    return cert;
};

var multiplexer = ConnectionMultiplexer.Connect(redisOptions);

But below error occurs.

Unhandled exception. StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). There was an authentication failure; check that passwords (or client certificates) are configured correctly: (AuthenticationException) The remote certificate is invalid because of errors in the certificate chain: PartialChain

If I use this code,

var certificate = X509Certificate2.CreateFromPemFile("server-ca.pem");

different error occurs.

Unhandled exception. System.Security.Cryptography.CryptographicException: The key contents do not contain a PEM, the content is malformed, or the key does not match the certificate.

How can I connect to redis server from .NET app with TLS encryption?

0

There are 0 best solutions below