Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 页面重定向_C#_Sql_Sql Server_Redirect - Fatal编程技术网

C# 页面重定向

C# 页面重定向,c#,sql,sql-server,redirect,C#,Sql,Sql Server,Redirect,登录成功后,如何从登录页面重定向到主页? 我有一个数据库,它存储的用户名和密码。 在登录时,它将通过sql查询检查用户名和密码。 我的代码如下所示 protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "") { Label3.Visible = true; Label3.Text = "* Requir

登录成功后,如何从登录页面重定向到主页? 我有一个数据库,它存储的用户名和密码。 在登录时,它将通过sql查询检查用户名和密码。 我的代码如下所示

protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "")
        {
            Label3.Visible = true;
            Label3.Text = "* Required Field";
        }
        else if (TextBox2.Text == "")
        {
            Label4.Visible = true;
            Label4.Text = "* Required Field";
        }

        else
        {
            Label3.Visible = false;
            Label4.Visible = false;
            userid = TextBox1.Text;
            pass = TextBox2.Text;

            SqlConnection conn = new SqlConnection("SERVER= server3\\OFFICESERVERS; Initial catalog = Web; Integrated Security = SSPI");
            SqlCommand mycmd = new SqlCommand();
            mycmd.Connection = conn;
            mycmd.CommandText = "SELECT FirstName, LastName, MiddleName, Email, Age FROM web WHERE IsActive=1 AND LoginName='" + userid + "' " + "AND Password='" + pass + "'"; 

            try
            {

                conn.Open();
                mycmd.ExecuteScalar();
                SqlDataAdapter da = new SqlDataAdapter(mycmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                GridView1.Visible=true;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                TextBox1.Text = "";
                TextBox2.Text="";


            }

            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }
    }
我的要求是,如果登录成功,我必须从登录页面重定向到主页,而不是gridview绑定。
如何完成它?

首先,看看如何使用存储过程!该SQL命令将使您面临SQL注入()的问题

如果我进去

  ' = '' or '1'='1
作为我的密码,它可以让我用我想要的任何用户名进入

其次,您可以只做一个Response.Redirect(“/relative/path/to/home.page”,false);将您重定向到主页

我会考虑重构代码,这样您就有了一些方法:

protected bool Login(string username, string password)  //handles logging the user in
protected void LoginSuccess() //handles the redirect if the user successfully logs in.
protected void BindDatagrid() //handles the databind if the user didn't log in.

首先,看看如何使用存储过程!该SQL命令将使您面临SQL注入()的问题

如果我进去

  ' = '' or '1'='1
作为我的密码,它可以让我用我想要的任何用户名进入

其次,您可以只做一个Response.Redirect(“/relative/path/to/home.page”,false);将您重定向到主页

我会考虑重构代码,这样您就有了一些方法:

protected bool Login(string username, string password)  //handles logging the user in
protected void LoginSuccess() //handles the redirect if the user successfully logs in.
protected void BindDatagrid() //handles the databind if the user didn't log in.

除了Mauro的答案之外,这里还有一些您可能需要考虑的其他变化:

  • 将Web控件重命名为更有意义的名称,例如txtPassword
  • 将连接字符串存储在Web.config文件中,这样您就可以更灵活地从测试过渡到生产
  • 在连接周围使用using语句,而不是try finally
  • SqlDataAdapter将处理关闭和打开连接的操作
  • 如果您使用的是SQL Server 2005或更高版本,则可以使用参数而不是SP(与内联SQL相比,SP的性能不会有太大提高)

  • 除了Mauro的答案之外,这里还有一些您可能需要考虑的其他变化:

  • 将Web控件重命名为更有意义的名称,例如txtPassword
  • 将连接字符串存储在Web.config文件中,这样您就可以更灵活地从测试过渡到生产
  • 在连接周围使用using语句,而不是try finally
  • SqlDataAdapter将处理关闭和打开连接的操作
  • 如果您使用的是SQL Server 2005或更高版本,则可以使用参数而不是SP(与内联SQL相比,SP的性能不会有太大提高)

  • 你的gridview毫无意义,好像登录不成功,它将不包含任何内容,如果登录成功,你将转到另一页。

    你的gridview毫无意义,好像登录不成功,它将不包含任何内容,如果登录成功,你将转到另一页。

    你击败了我。回答得好。你赢了我一拳。回答得好。