I'm trying to demo SQL Injection with a UNION query but I'm not getting results.
This is the C# code and the database is SQL Server 2008 R2:
string cString = "server=.\\sqlexpress; database=testDB; trusted_connection=true";
SqlConnection conn = new SqlConnection(cString);
conn.Open();
string sql = "select * from Users where UserName='" + userName + "' and Password='" + password + "'";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
SqlDataReader reader = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder(1024);
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
sb.Append(" " + reader.GetName(i));
sb.Append(": ");
sb.Append(reader[i]);
}
sb.Append("<br />");
}
dataLabel.Text = sb.ToString();
I have a username and password text boxes and the input is passed to this method.
I tried the following but no result:
' union select * from products --
the Users table and Products table have identical column types (int, nvarchar, nvarchar).
Can someone help? What am i missing?