I am trying to create a simple asp.net website that allows users to register and log in. I have successfully managed to store all the data in the database and authenticate the user in the log in form. However the thing I want to do now is whenever a new user registers to store the password in the database in MD5 format and match the hashes in order for the user to be able to login.
This is the code in the register section that stores the user in the database:
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["AssignmentDBConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into [AsTable] ([Username],Email,Password) values (@Username ,@Email, @Password)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Username", TextBoxUsername.Text);
com.Parameters.AddWithValue("@email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@password", TextBoxPass.Text);
com.ExecuteNonQuery();
Response.Redirect("Manager.aspx");
Response.Write("Registration Completed");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
And this is the code in the login section that authenticates the user in order for him to login:
conn.Open();
string checkPasswordQuery = "select Password from [AsTable] where Username ='" + TextBoxUsername.Text + "'";
SqlCommand passcom = new SqlCommand(checkPasswordQuery, conn);
string password = passcom.ExecuteScalar().ToString().Replace(" ","");
if (password == TextBoxPassword.Text)
{
Session["New"] = TextBoxUsername.Text;
Response.Write("Password is correct");
Response.Redirect("Index.aspx");
}
else
{
Response.Write("Password is not correct");
}
}
else
{
Response.Write("Username is not correct");
}
}
Any ideas what to change??