我在我的應用程序中使用PBKDF2來存儲用戶密碼。在我的用戶表,我這是這樣確定的Salt
和Password
柱:從表列讀取二進制到字節[] array
// 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
在我的登錄頁面,我需要找回這種鹽和密碼。因爲它們是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'中嗎 –
VS拋出一個錯誤,說它不能將long轉換爲byte [],並且參數描述與我放入的不一樣,比如'Salt ._Length'。 –