我在应用程序中使用PBKDF2来存储用户的密码。在我的用户表中,我有一个Salt
和Password
列,其确定方式如下:
// Hash the users password using PBKDF2
var DeriveBytes = new Rfc2898DeriveBytes(_Password, 20);
byte[] _Salt = DeriveBytes.Salt;
byte[] _Key = DeriveBytes.GetBytes(20); // _Key is put into the Password column
在我的登录页面上,我需要检索这个salt和密码。因为它们是byte[]数组,所以我将它们存储在我的表中作为varbinary(MAX)
。现在我需要检索它们以与用户输入的密码进行比较。我该如何使用SqlDataReader
来做到这一点?目前我有:
cn.Open();
SqlCommand Command = new SqlCommand("SELECT Salt, Password FROM Users WHERE Email = @Email", cn);
Command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = _Email;
SqlDataReader Reader = Command.ExecuteReader(CommandBehavior.CloseConnection);
Reader.Read();
if (Reader.HasRows)
{
// This user exists, check their password with the one entered
byte[] _Salt = Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);
}
else
{
// No user with this email exists
Feedback.Text = "No user with this email exists, check for typos or register";
}
但我可以确信地说这是错误的。在Reader
中的其他方法只有一个参数,即要检索的列的索引。
varbyte
吗? - Security HoundSalt._Length
。 - James Dawson