Reading binary from table column into byte[] array

前端 未结 2 1082
挽巷
挽巷 2021-01-05 05:48

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:

相关标签:
2条回答
  • 2021-01-05 06:25

    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
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-05 06:29

    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);

    0 讨论(0)
提交回复
热议问题