C# 从表列中读取二进制文件到 byte[] 数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13332960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:13:41  来源:igfitidea点击:

Reading binary from table column into byte[] array

c#asp.netarrayssql-server

提问by James Dawson

I'm using PBKDF2 in my application to store users passwords. In my Users table, I have a Saltand Passwordcolumn which is determined like this:

我在我的应用程序中使用 PBKDF2 来存储用户密码。在我的用户表中,我有一个SaltPassword列,它是这样确定的:

// 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

On my login page I need to retrieve this salt and password. Because they're byte[] arrays, I store them in my table as varbinary(MAX). Now I need to retrieve them to compare against the users entered password. How would I do that using SqlDataReader? At the moment I have this:

在我的登录页面上,我需要检索此盐和密码。因为它们是 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";
}

But I know for a fact that it's wrong. Other methods in Readerhave only one parameter being the index of the column to retrieve.

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

采纳答案by cmd.prompt

Casting it directly to a byte[]has worked for me so far.

byte[]到目前为止,将它直接投射到 a对我有用。

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
            }
        }
    }
}

回答by Blachshma

I'm not sure why you think the code you wrote is wrong (please explain). But specifically for the error:
Notice that GetBytesreturns a longnot a byte array.

我不确定你为什么认为你写的代码是错误的(请解释)。但专门针对错误:
请注意GetBytes返回一个long非字节数组。

So, you should use: Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);

所以,你应该使用: Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);

or
long bytesRead = Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);

或者
long bytesRead = Reader.GetBytes(0, 0, _Salt, 0, _Salt.Length);