我在 Visual Studio 2010 中开发了一个 Web 应用程序,并试图向该应用程序添加一个用户登录系统。到目前为止,我无法验证用户密码,因为程序告诉我所有密码都不正确,即使它与与该用户名关联的密码匹配。这是我给儿子写的代码:
protected void n_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
string cmdStr = "select count(*) from Registration where UserName='" + TextBoxUserName.Text + "'";
SqlCommand CheckUser = new SqlCommand(cmdStr, con);
int Temp = Convert.ToInt32(CheckUser.ExecuteScalar().ToString());
if (Temp == 1)
{
string cmdStr2 = "Select Password from Registration where UserName ='" + TextBoxUserName.Text + "'";
SqlCommand pass = new SqlCommand(cmdStr2, con);
string password = pass.ExecuteScalar().ToString();
con.Close();
if (password == TextBoxPassword.Text)
{
Session["New"] = TextBoxUserName.Text;
Response.Redirect("HomePage.aspx");
}
else
{
Label1.Visible = true;
Label1.Text = "Password is invalid";
}
}
else
{
Label1.Visible = true;
Label1.Text = "Username is invalid";
}
}
}
}
无论输入什么密码,程序都会输出“密码无效”,这表明问题出在 if 语句的第一部分?或者它使用的变量?可能还值得一提的是,无效的用户名会以同样的方式标记出来,这很好用。
提前致谢 :)