处理 IE 和 Firefox 中的 Cookie

发布于 2024-11-04 07:32:57 字数 3578 浏览 0 评论 0原文

我有一个应用程序,当用户登录该应用程序时,它会创建一个 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,它的工作方式与预期一致。

任何帮助将不胜感激。

谢谢!

I have an application that creates a cookie when the user logs into the application. The cookie is then read by subsequent pages and the user is allowed to continue processing on those pages if they are logged in (cookie exists). When I run my app on IE 8.0 the cookie responds as I expect. When I run my application through VS2008, the cookie responds as I expect. When I run my application in Firefox 4.0, the cookie is not valid on my next page.

Here is code setup of my 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 + "    <a href='AcctInfo.aspx'>Update Account Info</a>   <a href='TextAlerts.aspx'>Create/Update Text Alerts</a>   <a href='graphSetup.aspx'>Graphing</a>";
                lblMessage.Text = "";
                btnLogin.Enabled = false;
                btnLogoff.Enabled = true;
            }
            else
            {
                lblMessage.Text = "Login Unsuccessful";
            }
        }
    }
}

Here is my code for the on load of my next page which is 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() + "   <a href='TextAlerts.aspx'>Create/Update Text Alerts</a>   <a href='graphSetup.aspx'>Graphing</a>   ";
                    lblMenu.Text = strMenu;
                }
            }
        }
    }

Cookies are enabled in Firefox because I can remove the code that saves the cookie from the file it's in, put it into a separate file, browse that file and then browse my AcctInfo and it works like expected.

Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰ゝ天使的微笑 2024-11-11 07:32:57

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

Use Fiddler or other Http watcher tool to see what is actually send to/from browser. Code itself looks ok.

丢了幸福的猪 2024-11-11 07:32:57

您的 cookie 似乎已设置为立即过期,请尝试以下代码

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

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

It looks like your cookie is set to expire immeadiatly try the code below

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

        DateTime dtNow = DateTime.Now;
        cookie.Expires = DateTime.Now.AddYears(1);
        Response.Cookies.Add(cookie);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文