【问题标题】:redirection of pages页面重定向
【发布时间】:2023-03-20 09:25:02
【问题描述】:

登录成功后如何从登录页面重定向到主页? 我有一个数据库,它存储用户名和密码。 在登录时,它将通过 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 绑定。 怎么做?

【问题讨论】:

    标签: c# sql sql-server redirect


    【解决方案1】:

    首先,看看使用存储过程!该 SQL 命令让您对 SQL 注入问题敞开大门 (guard against SQL injection)

     mycmd.CommandText = "SELECT FirstName, LastName, MiddleName, Email, Age FROM web WHERE IsActive=1 AND LoginName='" + userid + "' " + "AND Password='" + pass + "'"; 
    

    如果我输入了

      ' = '' 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.
    

    【讨论】:

    • 你打败了我。很好的答案。
    【解决方案2】:

    除了 Mauro 的回答之外,还有一些您可能需要考虑的其他更改:

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

    【讨论】:

      【解决方案3】:

      你的gridview是没有意义的,好像登录不成功,它什么都没有,如果登录成功,你会转到另一个页面。

      【讨论】:

        猜你喜欢
        • 2018-09-11
        • 1970-01-01
        • 2017-09-09
        • 2012-10-29
        • 2010-11-09
        • 2015-05-25
        • 2011-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多