我有一个问题,当我运行一个SQL命令,结果是一个哈希密码SHA 256C#复制 '' 与SQL Server 2008
密码数据库:用C#返回"Z?VU??u2???f?[??\n?Mn??=1???<3?\v?"
密码查询后:"Z?VU??u2???f?[??\ \n?Mn??=1???<3?\ \v?"
(我把一个空间,因为它是由页面删除)
这是我的代码:
byte[] data = System.Text.Encoding.ASCII.GetBytes(txtPassword.Text);
data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data);
String hash = System.Text.Encoding.ASCII.GetString(data);
SqlDataAdapter da = new SqlDataAdapter();
string Command = "Select * From Users where Status=1 and Username='" + txtUser.Text + "'" + " and Password='" + hash + "'";
da.SelectCommand = new SqlCommand(Command, Database.Connection);
da.Fill(dsResult);
if (dsResult.Tables[0].Rows.Count != 0)
{
DoSomething();
}
我看到的错误,当我尝试这个
Select Password
From Users
Where Status = 1
And Username = '" + txtUser.Text + "'"
,结果是密码,但有重复\
。
在SQL Server Management Studio中,这个查询:
Select *
From Users
Where Status = 1
And Username = 'Rick'
And Password = 'Z?VU??u2???f?[??\n?Mn??=1???<3?\v?'
完美。
谢谢。
编辑:注射改变。
新代码:
SqlDataReader da;
string Command= @"Select * From Users where Status=1 and [email protected] and [email protected]";
SqlCommand cmd = new SqlCommand(Command, Database.Connection);
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = "Z?VU??u2???f?[??\n?Mn??=1???<3?\v?";
cmd.Parameters.Add("@User", SqlDbType.NVarChar).Value = txtUsuario.Text.Replace(" ", "");
da = cmd.ExecuteReader();
if (da.HasRows)
{
da.Read();
DoSomething();
}
但问题仍然存在。另外,在C#执行查询,解释,在数据库中的密码具有双斜杠,然后它永远不会与输入密码
你尝试设置参数的命令,而不是传递直接值? –
[SQL注入警报](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你应该**不**连接你的SQL语句 - 使用**参数化查询**,而不是避免SQL注入 - 检查[小Bobby表](https://xkcd.com/327/) –
复制反斜杠是**标准C#**行为 - 这并不罕见或导致关心。反斜杠被复制以表明你真的想要一个反斜杠 - 否则它将被解释为一个**转义序列**(与下一个字符一起,例如'\ t'是'','\ r'是回车等) –