我有一个使用 $hash = password_hash($accountpassword, PASSWORD_DEFAULT); 登录的主页。 (http://php.net/manual/de/function.password-hash.php)
密码保存为以 $2y$ 开头的哈希值。
然后我用 C# 创建了登录名:
//crypting the PW that user enter
string cryptedPassword = Crypter.Blowfish.Crypt(textBox_password.Text);
string user = textBox_username.Text;
string pass = cryptedPassword;
if (user == "" || pass == "")
{
MessageBox.Show("Empty Fields Detected ! Please fill up all the fields");
return;
}
bool r = validate_login(user, pass);
if (r)
MessageBox.Show("Correct Login Credentials");
else
MessageBox.Show("Incorrect Login Credentials"+cryptedPassword);
我的验证方法:
private bool validate_login(string user, string pass)
{
db_connection();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Select * from users where username=@user and password=@pass";
cmd.Parameters.AddWithValue("@user", user);
cmd.Parameters.AddWithValue("@pass", pass);
cmd.Connection = connect;
MySqlDataReader login = cmd.ExecuteReader();
if (login.Read())
{
connect.Close();
return true;
}
else
{
connect.Close();
return false;
}
}
Crypter.Blowfish.Crypt(textBox_password.Text) 已磨损。我变成了一个以$2a$开头的哈希
谁能帮忙解决这个问题吗?
最佳答案
当您使用Crypter.Blowfish.Crypt
方法时,您需要指定第二个参数盐,如果未提供,则会为您生成一个随机盐。显然,您将在后续调用中获得不同的随机值。
或者,您可以让 Crypter 库根据哈希值检查提供的密码:
if (Crypter.CheckPassword(password, cryptedPassword)) {
// Valid password code here
return true;
} else {
// Invalid password code here
return false;
}
您显然需要从数据库读取加密的密码,但我相信这种方法意味着您不需要为密码哈希提供/存储盐。
HTH
关于c# - 使用c#登录mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691766/