我正在使用Cookie在网站上登录用户。登录页面只有两个文本框(textbox9作为电子邮件ID,textbox10作为密码)。点击登录按钮后面的方法如下: -
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from stud where emaili='" + TextBox9.Text + "'", con);
con.Close();
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count == 0)
Label3.Text = "unknown user";
else if (dt.Rows[0][8].ToString() == TextBox9.Text && dt.Rows[0][3].ToString()==TextBox10.Text)
{
Label3.Text = "login success";
Response.Cookies["stdId"].Value = dt.Rows[0][0].ToString();
Response.Redirect("stdPortal.aspx");
}
else
{
Label3.Text = "invalid login";
}
}
用户的目标网页(stdportal.aspx)页面加载事件的代码如下所示,我在这里读取了Cookie: -
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
DataTable dt_stud = new DataTable();
DataTable dt_query = new DataTable();
DataTable dt = new DataTable();
public void create_table()
{
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "query";
dt_query.Columns.Add(col);
col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "resolved";
dt_query.Columns.Add(col);
}
int std_id;
protected void Page_Load(object sender, EventArgs e)
{
create_table();
if (Request.Cookies["stdId"] != null)
{
std_id = Convert.ToInt32(Request.Cookies["stdId"].Value);
conn.Open();
SqlDataAdapter sda_stud = new SqlDataAdapter("select * from stud where id='" + std_id + "'", conn);
SqlDataAdapter sda_query = new SqlDataAdapter("select * from query where std_id='" + std_id + "'", conn);
conn.Close();
sda_query.Fill(dt);
sda_stud.Fill(dt_stud);
Label1.Text = "Welcome " + dt_stud.Rows[0][1].ToString();
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt_query.NewRow();
row["query"] = dt.Rows[i][2].ToString();
row["resolved"] = dt.Rows[i][4].ToString();
dt_query.Rows.Add(row);
}
GridView1.DataSource = dt_query;
GridView1.DataBind();
}
else
Response.Redirect("default.aspx");
}
当我从localhost运行网站时,这些工作正常,但是当在GoDaddy上托管时,我得到了这些奇怪的行为: - 1)我使用注册的电子邮件和正确的密码登录,但它显示&#34;无效登录&#34;如果密码不匹配则设置 2)完成上述操作后,如果我使用相同的ID登录并将密码字段留空,我可以成功登录并在登录页面中正确显示我的姓名 3)然后,如果我返回登录页面并登录,同时将电子邮件ID和密码字段保留为空白,则以其他用户身份登录
我错误地使用了Cookie吗?如果是的话,我错在哪里?