I have a problem when I run a SQL command and the result is a hashed password sha 256
Password in database: "Z?VU??u2???f?[??\n?Mn??=1???<3?\v?"
Password returned by c# after the query: "Z?VU??u2???f?[??\ \n?Mn??=1???<3?\ \v?"
(I put a space because it is deleted by the page)
This is my code:
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();
}
I saw the error when I try this
Select Password
From Users
Where Status = 1
And Username = '" + txtUser.Text + "'"
and the result was the password but with duplicate \
.
In SQL Server Management Studio, this query:
Select *
From Users
Where Status = 1
And Username = 'Rick'
And Password = 'Z?VU??u2???f?[??\n?Mn??=1???<3?\v?'
works perfectly.
Thank you.
Edit: Injection Changes.
New Code:
SqlDataReader da;
string Command= @"Select * From Users where Status=1 and Username=@User and Password=@Password";
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();
}
But the problem persists. When C # executes the query interprets that the password in the database has the double slash then it never matches with the input password