[英]Verifying Hashed password
美好的一天,伙计们,
我有一个问题。 我已经能够使用 user_ID、用户名和密码字段创建用户帐户。
密码经过哈希处理和加盐处理,并且工作正常。 现在我想创建一个登录表单,用户需要在其中使用用户名和密码进行身份验证。
我想验证提供的密码和用户名是否正确。 这个密码必须先散列,然后与数据库中的内容进行比较。
下面是我的代码,但它不断带来密码错误的错误。
try
{
string connString = CommonVariables.ConnectionString;
// Hashing the password field first for it to be
string sql = "SELECT * FROM tbl_Users WHERE (Username = @Username) ";
using (SqlConnection cnn = new SqlConnection(connString))
{
cnn.Open();
using (SqlCommand cmd = new SqlCommand(sql, cnn))
{
//cmd.Parameters.AddWithValue("@Password", SqlDbType.NVarChar).Value = txt_Password.Text.Trim();
cmd.Parameters.AddWithValue("@Username", SqlDbType.NVarChar).Value = txt_Username.Text.Trim();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{
while (reader.Read())
{
// string vsibility = reader["Visibility"].ToString(); //Getting the value of the visibility to determine if the user can logon or not
// string user_role = reader["User_Role"].ToString(); // Getting the User_role of the person login on
string mypassword = reader["password"].ToString();
var hash = PasswordHashing.SecurePasswordHasher.Hash(mypassword);
var hashverify = PasswordHashing.SecurePasswordHasher.Verify(txt_Password.Text.Trim(), hash);
if (hashverify == true)
{
this.Hide();
new Mainmenu().Show(); ;
}
else
{
MessageBox.Show("incorrect password" + mypassword);
}
}
}
else
{
MessageBox.Show("Invalid Username, Please Confirm", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txt_Username.Focus();
return;
}
}
}
}
catch (Exception c)
{
MessageBox.Show(c.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
假设您将密码的散列存储在数据库中,您似乎是在对其进行双重散列。 特别是,该行
var hash = PasswordHashing.SecurePasswordHasher.Hash(mypassword);
似乎正在计算已经散列的密码的散列。 将此更改为var hash = mypassword;
可以帮助比较密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.