i have following authentication method:
protected void Button1_Click(object sender, EventArgs e)
{
string s;
s = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(s);
con.Open();
string sqlCmd;
sqlCmd = "SELECT Username, UserPassword FROM Benutzer WHERE Username = @Username AND UserPassword =@Password";
SqlCommand cmd = new SqlCommand(sqlCmd, con);
String username = tbUsername.Text.Replace("'", "''");
String password = tbPassword.Text.Replace("'", "''");
cmd.Parameters.AddWithValue("Username", username);
cmd.Parameters.AddWithValue("Password", password);
string CurrentName;
CurrentName = (string)cmd.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = cmd.Parameters[0].ToString();
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Benuztername/Password ungültig!";
}
}
is this enough to prevent sql injections? i used to just the username and password directly into the command like this:
sqlCmd = "SELECT Username, UserPassword FROM Benutzer WHERE Username ='" + username + "' AND UserPassword ='" + pwd + "'";
where username and pwd where just string variables in which the contents of username and password textboxes were stored...
EDIT:
ok i have edited my code which now looks like this:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection objcon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
SqlDataAdapter objda = new SqlDataAdapter("[MembershipPruefen]", objcon);
objda.SelectCommand.CommandType = CommandType.StoredProcedure;
objda.SelectCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = tbUsername.Text;
objda.SelectCommand.Parameters.Add("@UserPassword", SqlDbType.VarChar).Value = tbPassword.Text;
objcon.Open();
string CurrentName;
CurrentName = (string)objda.SelectCommand.ExecuteScalar();
if (CurrentName != null)
{
Session["UserAuthentication"] = tbUsername.Text;
Session.Timeout = 1;
Response.Redirect("Default.aspx");
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "Benuztername/Password ungültig!";
}
objcon.Close();
}
this is my stored procedure:
CREATE PROCEDURE MembershipPruefen (@Username VARCHAR(50), @UserPassword VARCHAR(50))
AS
SELECT Username, UserPassword FROM Benutzer WHERE Username LIKE @Username AND UserPassword LIKE @UserPassword;
is this sufficient? will my web app be secure against sql inections or is there still something to be done?