在IE和Firefox中处理Cookie

时间:2011-05-02 21:49:15

标签: c# asp.net internet-explorer cookies firefox4

我有一个应用程序,当用户登录应用程序时会创建一个cookie。然后,cookie由后续页面读取,如果用户已登录,则允许用户继续处理这些页面(存在cookie)。当我在IE 8.0上运行我的应用程序时,cookie会响应我的预期。当我通过VS2008运行我的应用程序时,cookie会响应我的预期。当我在Firefox 4.0中运行我的应用程序时,cookie在我的下一页上无效。

以下是我的Cookie代码设置

protected void btnLogin_Click(object sender, EventArgs e)
{
    if (CheckFields())
    {
        string strSQL;
        sqlUserInfo.SelectParameters.Clear();
        strSQL = "SELECT FirstName, LastName, Email FROM UserInfo WHERE Email=@email AND Password=@password";
        sqlUserInfo.SelectCommand = strSQL;
        if (txtEmail.Text != "")
        {
            sqlUserInfo.SelectParameters.Add("email", txtEmail.Text);
            sqlUserInfo.SelectParameters.Add("password", txtPassword.Text);
            DataView dv = (DataView)sqlUserInfo.Select(DataSourceSelectArguments.Empty);
            if (dv.Table.Rows.Count > 0)
            {
                string welcomeMsg = "Welcome back " + dv[0][0] + " " + dv[0][1] + "!";
                HttpCookie cookie = new HttpCookie("Email");
                cookie.Value = txtEmail.Text;

                DateTime dtNow = DateTime.Now;
                Response.Cookies.Add(cookie);

                lblMenu.Text = welcomeMsg + "&nbsp;&nbsp;&nbsp; <a href='AcctInfo.aspx'>Update Account Info</a>&nbsp;&nbsp;&nbsp;<a href='TextAlerts.aspx'>Create/Update Text Alerts</a>&nbsp;&nbsp;&nbsp;<a href='graphSetup.aspx'>Graphing</a>";
                lblMessage.Text = "";
                btnLogin.Enabled = false;
                btnLogoff.Enabled = true;
            }
            else
            {
                lblMessage.Text = "Login Unsuccessful";
            }
        }
    }
}

以下是我的下一页的加载代码,即acctinfo.aspx.cs

 protected void Page_Load(object sender, EventArgs e)
    {
        cookie = Request.Cookies["Email"];
        if (cookie == null)
        {
            lblMessage.Text = "Not logged in. <a href='login.aspx'>Login</a>";
            lbLogoff.Visible = false;
        }
        else
        {
            EnableControls();
            string strSQL;

            sqlUserInfo.SelectParameters.Clear();
            strSQL = "SELECT FirstName, LastName, Email, Password, PhoneNumber, Provider FROM UserInfo WHERE Email=@email";
            sqlUserInfo.SelectCommand = strSQL;
            sqlUserInfo.SelectParameters.Add("email", cookie.Value.ToString());
            DataView dv = (DataView)sqlUserInfo.Select(DataSourceSelectArguments.Empty);
            if (dv.Table.Rows.Count > 0)
            {
                oldPass = dv[0][3].ToString();
                oldPhone = dv[0][4].ToString();
                oldProvider = dv[0][5].ToString();
                if (!IsPostBack)
                {
                    txtPassword.Text = oldPass;
                    txtPassword2.Text = oldPass;
                    txtPhone.Text = oldPhone;
                    lstProvider.SelectedValue = oldProvider;
                    strMenu = "Welcome " + dv[0][0].ToString() + " " + dv[0][1].ToString() + "&nbsp;&nbsp;&nbsp;<a href='TextAlerts.aspx'>Create/Update Text Alerts</a>&nbsp;&nbsp;&nbsp;<a href='graphSetup.aspx'>Graphing</a>&nbsp;&nbsp;&nbsp;";
                    lblMenu.Text = strMenu;
                }
            }
        }
    }

在Firefox中启用了Cookie,因为我可以删除将Cookie保存到其中的文件的代码,将其放入单独的文件中,浏览该文件,然后浏览我的AcctInfo,它就像预期的那样工作。

非常感谢任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:1)

使用Fiddler或其他Http观察工具查看实际发送到浏览器或从浏览器发送的内容。代码本身看起来不错。

答案 1 :(得分:0)

看起来您的Cookie设置为过期,尝试下面的代码

HttpCookie cookie = new HttpCookie("Email"); 
        cookie.Value = txtEmail.Text;

        DateTime dtNow = DateTime.Now;
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);