I\'m using PBKDF2 in my application to store users passwords. In my Users table, I have a Salt
and Password
column which is determined like this:
Casting it directly to a byte[]
has worked for me so far.
using (SqlConnection c = new SqlConnection("FOO"))
{
c.Open();
String sql = @"
SELECT Salt, Password
FROM Users
WHERE (Email = @Email)";
using (SqlCommand cmd = new SqlCommand(sql, c))
{
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = _Email;
using (SqlDataReader d = cmd.ExecuteReader())
{
if (d.Read())
{
byte[] salt = (byte[])d["Salt"];
byte[] pass = (byte[])d["Password"];
//Do stuff with salt and pass
}
else
{
// NO User with email exists
}
}
}
}
I'm not sure why you think the code you wrote is wrong (please explain). But specifically for the error:
Notice that GetBytes returns a long
not a byte array.
So, you should use:
Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);
or
long bytesRead = Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);