使用会话和本地数据库进行c#登录

时间:2019-07-17 15:26:51

标签: c# database

我在tblRegister表中有2个用户,这些用户具有不同的“类型”(数据类型为nvarchar,例如“我是一名教师”和“我是一名学生”)。当我运行此登录代码时,页面保持不变。登录后,我必须将每个用户类型重定向到另一个网页,但这似乎不起作用。

(....)私有字符串_conString = WebConfigurationManager.ConnectionStrings [“ thCS”]。ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Cookies["un"] != null && Request.Cookies["pwd"] != null)
            {
                txtusername.Text = Request.Cookies["un"].Value;
                txtpassword.Attributes["value"] = Request.Cookies["pwd"].Value;
                //txtPassword.Text = Request.Cookies["pwd"].Value;
            }
        }
    }

    protected void Login_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(_conString);

        // Create Command
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        //searching for a record containing matching username, password and status         
        cmd.CommandText = "SELECT * FROM tblRegister where username=@username and password=@password and status=@status";

        cmd.Parameters.AddWithValue("@username", txtusername.Text);
        cmd.Parameters.AddWithValue("@passwordd", decrypt.Encrypt(txtpassword.Text));
        cmd.Parameters.AddWithValue("@status", 1);


        //Create DataReader
        SqlDataReader reader;
        con.Open();
        reader = cmd.ExecuteReader();

        // check if username, password and status in reader exists in DB         
        if (reader.HasRows)
        {
            if (reader.Read())
            {
                Response.Cookies["un"].Value = txtusername.Text;
                Response.Cookies["pwd"].Value = txtpassword.Text;

                if (chkremember.Checked)
                {
                    Response.Cookies["un"].Expires = DateTime.Now.AddDays(100);
                    Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(100);
                }
                else
                {
                    Response.Cookies["un"].Expires = DateTime.Now.AddDays(-100);
                    Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(-100);
                }

                String rtype = reader["type"].ToString().Trim();
                if (rtype == "i am a tutor")
                {
                    Session["username"] = reader["username"];
                    Response.Redirect("updateprofile.aspx");
                }
                else
                {
                    Session["student"] = reader["username"];
                    Response.Redirect("dashboard.aspx");
                }

            }

            con.Close();

        }

        else
        {
            lblstatus.Text = "You are not registered or your account has been suspended!";

        }
    }

0 个答案:

没有答案