检查 MySqlConnection 是否使用 SSL

检查 MySqlConnection 是否使用 SSL

问题描述:

我认为我的问题类似于 C#:如何检查 MySqlConnection 是否使用 SSL?,但不幸的是它没有很好的答案,因为它可能不清楚.所以这是我的看法:

I think my question is similar to C#: how to check if a MySqlConnection is using SSL or not?, but unfortunately it doesn't have good answers because it is unclear maybe. So here's my take:

我创建了一个新连接:

var connection = new MySqlConnection("Data Source=example.com;Port=3306;Database=Foo;User Id=root;Password=foo;SSL Mode=Required");

我如何验证它使用 SSL,是否有类似 connection.IsOverSSL 的东西?

How do I verify it uses SSL, is there something like connection.IsOverSSL?

我尝试使用 SHOW SESSION STATUS LIKE 'Ssl_cipher',但这给了我 Ssl_cipher 即使 SSL Mode=Required:

I tried using SHOW SESSION STATUS LIKE 'Ssl_cipher', but this gives me Ssl_cipher even if SSL Mode=Required:

我使用的代码是:

var connection = new MySqlConnection(ConfigurationManager.AppSettings["Test"]);
connection.Open();
var command = new MySqlCommand("SHOW SESSION STATUS LIKE \'Ssl_cipher\'", connection);
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine(reader.GetString(0));
}

根据https://dev.mysql.com/doc/refman/5.7/en/using-encrypted-connections.html,它应该给我 Ssl_cipher |DHE-RSA-AES128-GCM-SHA256

您可以查看SSL"一词的连接字符串.这将让您知道在这种情况下您的 IDbConnection 是否使用 ssl.有关可用于 MySql 的连接字符串列表,请访问 ConnectionStrings.com.如果这能解决您的问题,请告诉我.

You can look at the connection string for the word "SSL". This would allow you to know if your IDbConnection is using ssl in this case. For a list of the connection strings that you could use with MySql, please visit ConnectionStrings.com. Let me know if this solves your problem.

我会尝试查看连接字符串.这是一个证明这一点的解决方案.

I would try looking at the connection string. Here is a solution to demonstrate this.

var connStr = "Data Source=example.com;Port=3306;Database=Foo;User Id=root;Password=foo;SSL Mode=Required";
var sslElement = connStr.Split(';')
    .SingleOrDefault(s => s.StartsWith("SSL", StringComparison.InvariantCultureIgnoreCase));

var sslModeEnabled = (sslElement != null 
    && string.Equals(sslElement.Split('=')[1].Trim(), "None", StringComparison.InvariantCultureIgnoreCase) == false);


Console.WriteLine($"SSL Mode Enabled: {sslModeEnabled}");