登录后从数据库中获取用户特定信息

时间:2012-06-15 14:24:30

标签: c# asp.net database passwords

我正在做一个项目,我需要为学校网站制作一个VLE系统。我正在使用可视化Web开发人员2008,C#和SQL数据库。我需要做的是当学生使用他们的用户名和密码登录时,他们应该从数据库中显示他们的特定信息(比如SQL中的课程名称,学生ID,电子邮件和姓名等)我创建了登录页面:

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString);
    con.Open();
    string cmdStr = "select count (*) from Registration where UserName ='" + TextBox1.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 ='" + TextBox1.Text + "'";
        SqlCommand pass = new SqlCommand(cmdStr2, con);
        String password = pass.ExecuteScalar().ToString();
        con.Close();

        if (password == TextBox2.Text)
        {
            Session["New"] = TextBox1.Text;
            Response.Redirect("secure.aspx");
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "Invalid Password....!!!";
        }
    }
    else
    {
        Label1.Visible = true;
        Label1.Text = "Invalid UserName....!!!";
    }
}

2 个答案:

答案 0 :(得分:1)

当您获得用户名时,您可以转到数据集以检索有关该用户的所有详细信息,并相应地在secure.aspx中显示它。

建议:不要直接使用sql查询(易受sql注入攻击),你应该使用存储过程.. 并且也不要以明文形式存储密码。 为什么要为相同的数据检索两次数据。

答案 1 :(得分:0)

这不会解决你所有的问题,但我会读这篇文章

http://www.codehighstreet.com/Articles/overview_of_membership_and_installing_sql_membership_provider-part_1/overview_of_membership_and_installing_sql_membership_provider-part_1.aspx

您当前的用户名和密码解决方案根本不是很安全,因为您当前将密码存储为纯文本。 本文介绍如何安装和设置用户名和密码数据库,并使用一些将为您处理身份验证的控件