C# 页面重定向
登录成功后,如何从登录页面重定向到主页? 我有一个数据库,它存储的用户名和密码。 在登录时,它将通过sql查询检查用户名和密码。 我的代码如下所示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
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的答案之外,这里还有一些您可能需要考虑的其他变化:
除了Mauro的答案之外,这里还有一些您可能需要考虑的其他变化:
你的gridview毫无意义,好像登录不成功,它将不包含任何内容,如果登录成功,你将转到另一页。你的gridview毫无意义,好像登录不成功,它将不包含任何内容,如果登录成功,你将转到另一页。你击败了我。回答得好。你赢了我一拳。回答得好。