I try connect to a MySql (version 5.0.95) database from a ASP.NET (Razor) web page...
(Assembly MySql.Data.dll, v6.6.5.0)
First I tried directly to specify directly the password in the connection string like "...;pwd=myClearPassword"
. But When trying to Open obtained the error
"Authentication with old password no longer supported, use 4.1 style passwords."
So I try now to update my code, like this:
@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;
}
However I obtain the same error. Where is the problem?
PS.
On the SQL server, the old_passwords
variable is set to ON
(default value is OFF). We don't control the server so this variable should remain unchanged.
NB.
There is a lot of questions with the same title. However please not to make this question as duplicate, because of different context of the question. I was mainly inspired from this answer for the code above...