基于角色的登录不会对管理员进行身份验证

时间:2017-09-26 10:54:00

标签: c# sql asp.net

所以我试图根据他们的角色重定向每个用户我有用户和管理员都从同一页面登录,但在我的情况下,它只验证用户而不是管理员。它显示我的“您输入了无效的用户名或密码”消息。有什么想法吗。感谢

这是我的代码

protected void Button1_Click(object sender, EventArgs e)
{
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "select * from registration where email='"+ TextBox1.Text+"' and password='"+TextBox2.Text+"'";
    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    tot = Convert.ToInt32(dt.Rows.Count.ToString());

    if (tot > 0)
    {
        if (Session["checkoutbutton"] == "yes")
        {
            Session["user"] = TextBox1.Text;
            Response.Redirect("update_order_details.aspx");
        }
        else
        {
            Session["user"] = TextBox1.Text;
            Response.Redirect("order_details.aspx");
        }

    }
    else
    {
        Label1.Text = "Invalid email or password";
    }
    con.Close();

    con.Open();
    SqlCommand cmd1 = con.CreateCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "select * from admin_login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "' ";
    cmd1.ExecuteNonQuery();
    DataTable dt1 = new DataTable();
    SqlDataAdapter da1 = new SqlDataAdapter(cmd);
    da1.Fill(dt);
    i = Convert.ToInt32(dt.Rows.Count.ToString());
    if (i == 1)
    {
        Session["admin"] = TextBox1.Text;
        Response.Redirect("add_product.aspx");
    }
    else
    {
        Label1.Text = "you have entered invalid username or password";
    }
    con.Close();
}

2 个答案:

答案 0 :(得分:1)

if (i == 1)
    {
        Session["admin"] = TextBox1.Text;
        Response.Redirect("add_product.aspx");
    }
    else
    {
        Label1.Text = "you have entered invalid username or password";
    }

您是否检查过是否有1行或0行?

答案 1 :(得分:0)

您错误地为管理员传递了cmd对象,它应该是cmd1。数据表也应该是dt1而不是dt for admin

int i;
con.Open();
SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select count(*) from admin_login where username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "' ";
i = cmd1.ExecuteScalar();
if (i == 1)
{
    Session["admin"] = TextBox1.Text;
    Response.Redirect("add_product.aspx");
}
else
{
    Label1.Text = "you have entered invalid username or password";
}
con.Close();