Reading binary from table column into byte[] array

前端 未结 2 1101
挽巷
挽巷 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
                }
            }
        }
    }
    

提交回复
热议问题