哈希 MySql 密码给出:"Authentication with old password no longer supported, use 4.1 style passwords"

Hashing MySql passwords gives: "Authentication with old password no longer supported, use 4.1 style passwords"

我尝试从 ASP.NET (Razor) 网页连接到 MySql(版本 5.0.95)数据库...
(程序集 MySql.Data.dll, v6.6.5.0)

首先我尝试直接在连接字符串中直接指定密码,如"...;pwd=myClearPassword"。但是在尝试打开时出现错误

"Authentication with old password no longer supported, use 4.1 style passwords."

所以我现在尝试更新我的代码,如下所示:

@using MySql.Data.MySqlClient
@using System.Security.Cryptography

private const string ConnectionStringFormat = 
   "server=xxx.xx.xxx.xx;database=mydbname;uid=username;pwd={0};";      

private string GetHashedPassword(string clearPassword)
{
    var hasher = new SHA256Managed();
    var unhashedPassword = System.Text.Encoding.Unicode.GetBytes(clearPassword);
    var hashedBytes = hasher.ComputeHash(unhashedPassword);
    var hashedPassword = Convert.ToBase64String(hashedBytes);

    return hashedPassword;
}

public Dictionary<int, string> GetIdStringCollectionFromTable(string tableName)
{
    var hasehdPassword = GetHashedPassword("myClearPassword");
    var connectionString = string.Format(ConnectionStringFormat, hasehdPassword);
    MySqlConnection conn = new MySqlConnection(connectionString);
    conn.Open(); // >>>>> ERROR !!!!!!!!

    var sqlCommand = "select id, name from {0}".Fill(tableName);
    MySqlCommand command = new MySqlCommand(sqlCommand, conn);
    var reader = command.ExecuteReader();

    Dictionary<int, string> list = new Dictionary<int, string>();
    while (reader.Read())
    {
        int id = reader.GetInt32(0);
        string text = reader.GetString(1);
        list.Add(id, text);
    }
    return list;
}

但是我得到了同样的错误。问题出在哪里?

PS.

在 SQL 服务器上,old_passwords 变量设置为 ON(默认值为 OFF)。我们不控制服务器,所以这个变量应该保持不变。

注意.
有很多同标题的问题。但是,由于问题的上下文不同,请不要将此问题重复。对于上面的代码,我的灵感主要来自 this answer...

首先,关于一般身份验证的几件事;

开发应用程序时通常使用两种类型的身份验证:

数据库身份验证 - 这是应用程序验证访问数据库的方式

用户身份验证 - 这是用户向您的应用程序进行访问身份验证的方式

您link上面的文章是在谈论用户身份验证,而您的问题实际上是关于数据库身份验证。

MySQL(4.1 之前)使用的原始哈希算法被认为是不安全的。 4.1 版实现了一种新的散列算法。连接字符串中的密码不需要散列,散列是在对数据库进行身份验证期间在 .Net 连接器内部执行的(已为您完成)。问题是,如果您已将数据库从 4.1 之前的版本升级,并且没有重置密码以使用新的哈希算法。

您可以采取以下两种方法之一来纠正这种情况。这些脚本在数据库中 运行。

  1. 允许数据库接受旧式散列运行

设置old_passwords=真

  1. 使用新的哈希设置新密码

设置old_passwords=假

设置密码=密码('your_new_password_here')

建议使用第二种方法,使用新的哈希算法,因为它使您的数据库访问更加安全。