I am writing some server-side validation in asp.net for login page.
Now, I am coming from a "write it from scratch" PHP perspective and i am learning and struggling with some of these asp.net concepts which i'm unaware of.
I am trying to set a username and password variable to "valid" if the input is valid, and i am trying to use these variables to proceed with login.
I'm also not sure if this is the correct way to do things.
protected void loginbutton_Click(object sender, EventArgs e)
{
string UsernameRegex = "[a-zA-Z]+";
string PasswordRegex = "[a-zA-Z0-9]+";
if (!Regex.IsMatch(usernametextbox.Text, UsernameRegex))
{
string UsernameCheck = "valid";
}
else
{
string UsernameCheck = "invalid";
}
if (!Regex.IsMatch(passwordtextbox.Text, PasswordRegex))
{
string PasswordCheck = "valid";
}
else
{
string PasswordCheck = "invalid";
}
if(UsernameCheck = "valid") //i will include password here after i solved the problem
{
//do something
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();
string checkuser = "select count(*) from Users where Username = @username and Password = @password";
SqlCommand com = new SqlCommand(checkuser, conn);
com.Parameters.Add("@username", SqlDbType.NVarChar).Value = usernametextbox.Text;
com.Parameters.Add("@password", SqlDbType.NVarChar).Value = passwordtextbox.Text;
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp > 0)
{
Response.Redirect("Cars.aspx");
}
else
{
loginfaillabel.Text = "Your Username or Password doesn't match our records";
}
}
Help and feedback is appreciated.