gpt4 book ai didi

C# Mysql datareader - 如何使用 datareader 获取其他列

转载 作者:行者123 更新时间:2023-11-29 06:27:34 25 4
gpt4 key购买 nike

我正在尝试验证我的数据库中经过哈希处理和加盐处理的密码。我在用户表用户名、哈希值和盐中创建了一列。现在我想知道我可以使用数据读取器访问其他列值。

我尝试了这个方法,但出现了一条红线。这也是我失败的尝试

    public static bool VerifyPassword(string enteredPassword, string storedHash, string storedSalt)
{

var saltBytes = Convert.FromBase64String(storedSalt);
var rfc2898DeriveBytes = new Rfc2898DeriveBytes(enteredPassword, saltBytes, 10000);
return Convert.ToBase64String(rfc2898DeriveBytes.GetBytes(256)) == storedHash;

}


private void bunifuFlatButton1_Click(object sender, EventArgs e)
{

string userhash;
string usersalt;

MySqlConnection mysqlCon = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM login.info WHERE username = @user", mysqlCon);
MySqlDataReader rd;
rd = cmd.ExecuteReader();
cmd.Parameters.Add("@user", MySqlDbType.VarChar).Value = username.Text;
mysqlCon.Open();

while (rd.Read())
{

userhash = rd.GetString("hash");
usersalt = rd.GetString("salt");

bool isPasswordMatched = VerifyPassword(textpass.Text, userhash.Hash, usersalt.Salt);
// i got redline error in here. i only follow instruction.. link below
if (isPasswordMatched)
{
//Login Successfull
}
else
{
//Login Failed
}
}
}

顺便说一下,我只遵循这个线程中的这条指令。 How to validate salted and hashed password in c#

最佳答案

这是编写代码的另一种方式,不是真正的答案,但是......不是完美的头脑,但至少它会处理对象并以正确的顺序调用它们。请阅读 IDisposable 和 Sql 注入(inject)。

private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
using (MySqlConnection mysqlCon = new MySqlConnection(connectionString))
{
// Use a named list of fields please. And cleanse the text.
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM login.info WHERE username = @user", mysqlCon))
{
cmd.Parameters.Add("@user", MySqlDbType.VarChar).Value = username.Text; // Let's hope user name is not Jimmy DropTables!!
mysqlCon.Open();

using (MySqlDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
string userhash = rd.GetString("hash");
string usersalt = rd.GetString("salt");

bool isPasswordMatched = VerifyPassword(textpass.Text, userhash, usersalt);
// Note that we are passing in strings, not props of an unknown object
if (isPasswordMatched)
{
//Login Successfull
}
else
{
//Login Failed
}
}
}

mysqlCon.Close();
}
}
}

关于C# Mysql datareader - 如何使用 datareader 获取其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58608662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com