检查MySqlConnection是否使用SSL

时间:2017-10-06 09:10:05

标签: c# mysql ssl

我认为我的问题类似于C#: how to check if a MySqlConnection is using SSL or not?,但遗憾的是它并没有很好的答案,因为可能还不清楚。所以这是我的看法:

我创建了一个新连接:

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

如何验证它是否使用SSL,是否有类似connection.IsOverSSL的内容?

修改

我尝试使用SHOW SESSION STATUS LIKE 'Ssl_cipher',但即使Ssl_cipher,这也会给我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

2 个答案:

答案 0 :(得分:0)

您可以查看单词“SSL”的连接字符串。这将允许您知道在这种情况下您的IDbConnection是否正在使用ssl。有关可以与MySql一起使用的连接字符串的列表,请访问ConnectionStrings.com。如果这可以解决您的问题,请告诉我。

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

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}");

答案 1 :(得分:0)

原来我没有打印查询的第二列,现在可行了:

rails aborted!
LoadError: /home/bassemustafa/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9/lib/mysql2/mysql2.so: undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE - /home/bassemustafa/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/mysql2-0.4.9/lib/mysql2/mysql2.so
/home/bassemustafa/rails/depot5/config/application.rb:7:in `<top (required)>'
/home/bassemustafa/rails/depot5/Rakefile:4:in `require_relative'
/home/bassemustafa/rails/depot5/Rakefile:4:in `<top (required)>'
/home/bassemustafa/rails/depot5/bin/rails:9:in `require'
/home/bassemustafa/rails/depot5/bin/rails:9:in `<top (required)>'
/home/bassemustafa/rails/depot5/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
(See full trace by running task with --trace)  

它为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)}: {reader.GetString(1)}"); } 打印Ssl_cipher: AES256-SHA,为SSL Mode=Required打印Ssl_cipher:。另一方面,即使SSL Mode=None完全丢失,我也会Ssl_cipher: AES256-SHA,所以默认情况下它可能会启用。