加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从表列读取二进制到byte []数组

发布时间:2020-12-15 08:39:50 所属栏目:百科 来源:网络整理
导读:我在我的应用程序中使用PBKDF2来存储用户密码.在我的Users表中,我有一个Salt和Password列,其确定方式如下: // Hash the users password using PBKDF2var DeriveBytes = new Rfc2898DeriveBytes(_Password,20);byte[] _Salt = DeriveBytes.Salt;byte[] _Key
我在我的应用程序中使用PBKDF2来存储用户密码.在我的Users表中,我有一个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

在我的登录页面上,我需要检索此盐和密码.因为它们是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,_Salt,_Salt.Length);
}
else
{
    // No user with this email exists
    Feedback.Text = "No user with this email exists,check for typos or register";
}

但我知道这是错误的事实. Reader中的其他方法只有一个参数是要检索的列的索引.

谢谢!

解决方法

到目前为止,将它直接转换为byte []对我有用.
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
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读