登录无效

时间:2011-05-01 16:40:03

标签: c# asp.net

我已经为登录页面编写了以下代码,但似乎无法正常工作 变量temp的值为0

protected void ButtonSbmt_Click(object sender, EventArgs e) {
    //if (IsPostBack) {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
    conn.Open();
    string cmdstr = "select count(*) from Registration where username='" + TextBoxUsername.Text + "'";
    SqlCommand checkuser = new SqlCommand(cmdstr, conn);       

    int temp = Convert.ToInt32(checkuser.ExecuteScalar().ToString());

    if (temp == 1) {

        string str = "select password from Registration where username='" + TextBoxUsername.Text + "'";

        SqlCommand pass = new SqlCommand(str, conn);
        string pass1 = pass.ExecuteScalar().ToString();
        conn.Close();

        if (pass1 == TextBoxPassword.Text) {
            Session["new"] = TextBoxUsername.Text;
            Response.Redirect("secure.aspx");
        } else {
            Label1.Visible = true;
            Label1.Text = "invalid password";
        }     
    }
}

1 个答案:

答案 0 :(得分:5)

使用ASP.NET成员资格提供程序和ASP.NET登录控件以及ASP.NET表单身份验证。这是ASP.NET提供的内置功能;它有效,它是安全的,你不必编写SQL语句和逻辑。

关于安全性的第一课 - 如果有内置功能,请使用它。它总是比开始自己编写更好。