c# - 使用c#登录mysql

标签 c# mysql authentication passwords

我有一个使用 $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/

相关文章:

c# - 如何根据类型列表检查对象的类型?

ajax - Java EE的RESTful身份验证

c# - 强制 SMTP 服务器仅通过 TLS 发送邮件

c# - 从 Form App c# 启动停止服务

c# - 忽略复杂类型的一个属性

mysql - Shield UI 网格日期 : how to format

php - 使用 laravel 按纬度和经度搜索

java - 如何在XML中存储和访问大量且经常使用的数据?

c# - 如何修复 'IdentityModel.dll not found' ?

java - 来自不同网址时的不同登录