通过StackExchange与Redis建立SSL连接,

fdbelqdn  于 2022-11-14  发布在  Redis
关注(0)|答案(3)|浏览(265)

我有一个非常奇怪的问题与StackExchange。Redis连接到Redis。
我已经在Redis数据库上启用了SSL,但我无法使用SSL证书从客户端连接到Redis服务器,代码如下。

  1. static RedisConnectionFactory()
  2. {
  3. try
  4. {
  5. string connectionString = "rediscluster:13184";
  6. var options = ConfigurationOptions.Parse(connectionString);
  7. options.Password = "PASSWORD";
  8. options.AllowAdmin = true;
  9. options.AbortOnConnectFail = false;
  10. options.Ssl = true;
  11. options.SslHost = "HOSTNAME";
  12. var certificate = GetCertificateFromThubprint();
  13. options.CertificateSelection += delegate
  14. {
  15. return certificate;
  16. };
  17. Connection = new Lazy<ConnectionMultiplexer>(
  18. () => ConnectionMultiplexer.Connect(options)
  19. );
  20. }
  21. catch (Exception ex)
  22. {
  23. throw new Exception("Unable to connect to Cache Server " + ex);
  24. }
  25. }
  26. public static ConnectionMultiplexer GetConnection() => Connection.Value;
  27. public static IEnumerable<RedisKey> GetCacheKeys()
  28. {
  29. return GetConnection().GetServer("rediscluster", 13184).Keys();
  30. }
  31. // Find certificate based on Thumbprint
  32. private static X509Certificate2 GetCertificateFromThubprint()
  33. {
  34. // Find certificate from "certificate store" based on thumbprint and return
  35. StoreName CertStoreName = StoreName.Root;
  36. string PFXThumbPrint = "NUMBER";
  37. X509Store certLocalMachineStore = new X509Store(CertStoreName, StoreLocation.LocalMachine);
  38. certLocalMachineStore.Open(OpenFlags.ReadOnly);
  39. X509Certificate2Collection certLocalMachineCollection = certLocalMachineStore.Certificates.Find(
  40. X509FindType.FindByThumbprint, PFXThumbPrint, true);
  41. certLocalMachineStore.Close();
  42. return certLocalMachineCollection[0];
  43. }

但是,如果我创建一个控制台应用程序,并使用上面的代码连接到Redis,那么我就可以连接,但是如果我使用我的Web应用程序中的相同代码连接到Redis,那么我就无法连接。
不确定我是否错过了什么。
还有我看了“mgravell”的帖子
在那篇文章中,他配置了“CertificateValidation”方法,在我的场景中,我想让Redis验证SSL证书。所以我没有实现验证。并实现了“CertificateSelection”方法来提供客户端证书。

irtuqstp

irtuqstp1#

您可以尝试使用CertificateValidation来验证证书。我尝试了以下代码,它对我很有效:

  1. options.CertificateValidation += ValidateServerCertificate;

...

  1. public static bool ValidateServerCertificate(
  2. object sender,
  3. X509Certificate certificate,
  4. X509Chain chain,
  5. SslPolicyErrors sslPolicyErrors)
  6. {
  7. if (sslPolicyErrors == SslPolicyErrors.None)
  8. return true;
  9. Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
  10. return false;
  11. }
展开查看全部
shstlldc

shstlldc2#

在这样的情况下,您使用的是客户端证书,它在控制台应用程序中工作,但在其他应用程序中不工作(您没有说,但我猜是来自IIS托管的Web应用程序),它几乎总是与帐户是否有权访问私钥有关。
控制台应用程序使用您的帐户运行,该帐户可能可以访问私钥。
给予帐户访问权限
1.打开本地计算机证书存储
1.查找您客户端证书
1.右键单击并选择“所有任务-〉管理验证密钥...”
1.单击“添加...”并添加帐户。
注意:如果添加IIS应用程序池帐户,则格式为:IIS APPPOOL〈我的应用程序集区名称〉
位置应该是本地计算机而不是域。

jhdbpxl9

jhdbpxl93#

我可以用下面的代码对我在虚拟机上启动的Redis服务器进行ssl。add stackexchange.redis Visual Studio

  1. try
  2. {
  3. ConfigurationOptions configurationOptions = new ConfigurationOptions
  4. {
  5. KeepAlive = 0,
  6. AllowAdmin = true,
  7. EndPoints = { { "SERVER IP ADDRESS", 6379 }, { "127.0.0.1", 6379 } },
  8. ConnectTimeout = 5000,
  9. ConnectRetry = 5,
  10. SyncTimeout = 5000,
  11. AbortOnConnectFail = false,
  12. };
  13. configurationOptions.CertificateSelection += delegate
  14. {
  15. var cert = new X509Certificate2("PFX FILE PATH", "");
  16. return cert;
  17. };
  18. ConnectionMultiplexer connection =
  19. ConnectionMultiplexer.Connect(configurationOptions);
  20. IDatabase databaseCache = connection.GetDatabase();
  21. //set value
  22. databaseCache.StringSet("KEYNAME", "KEYVALUE");
  23. //get Value
  24. label_show_value.Text = databaseCache.StringGet("KEYNAME").ToString();
  25. }
  26. catch (Exception e1)
  27. {
  28. }
展开查看全部

相关问题