【问题标题】:Cookie vanishing饼干消失
【发布时间】:2014-10-08 19:24:12
【问题描述】:

我正在为客户开发登录和用户数据库系统。我遇到的问题是我正在尝试注册用户设备,以限制给定用户在一个月内可以使用多少设备登录。这一切都很好,但是我在客户端计算机上保存的 cookie 存在问题。

即使我在下面的代码中设置了过期日期,Internet Explorer 也会在会话结束时删除 cookie。它在 Chrome 中运行良好。 设置 cookie 代码:

                cookie = new HttpCookie("DeviceLog");
                cookie.Expires = DateTime.Now.AddDays(30);
                cookie.Values.Add("DeviceId", guid.ToString());
                Response.Cookies.Add(cookie);

检索 cookie 代码:

                HttpCookie cookie = Request.Cookies["DeviceLog"];

我承认我对 cookie 的了解是有限的,所以这确实是相当不言而喻的事情,但是即使在 Google 上一个小时后我也无法解决它:p

更新,整个 cookie 代码 sn-p:

        protected void Page_Load(object sender, EventArgs e) {
        if (User.Identity.IsAuthenticated) {
            Response.Redirect(STSUtility.GetRefereUrl(true));
        }
        if (!IsPostBack) {
            try {
                if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null) {
                    username.Text = Request.Cookies["UserName"].Value;
                    password.Attributes["value"] = AESEncrypter.Decrypt(Request.Cookies["Password"].Value);
                    remember.Checked = true;
                }
            } catch (Exception) {
                Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);
            }
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e) {
        if (remember.Checked) {
            Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(30);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
        } else {
            Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);

        }
        Response.Cookies["UserName"].Value = username.Text.Trim();
        Response.Cookies["Password"].Value = AESEncrypter.EncryptToken(password.Text.Trim());

        MembershipProvider mp = new MembershipProvider();
        bool userValidated = mp.ValidateUser(username.Text, password.Text);

        if (userValidated && Page.IsValid) {
            HttpCookie cookie = Request.Cookies["DeviceLog"];
            ContextDataContext model = new ContextDataContext();
            User user = model.Users.First(x => x.Email == username.Text.Trim());

            Session["UserId"] = user.Id.ToString();
            int userDevices = model.UserDevicesLogs.Count(x => x.UserId == user.Id);

            if (!user.Active) {
                return;
            }

            if (userDevices >= maxDeviceLoginsPrUser && cookie == null) {
                Response.Redirect("/Sites/Login/ExceedMaxDevices.aspx");
            }

            if (cookie == null) {//Hvis device ikke har en cookie
                Guid guid = Guid.NewGuid();

                cookie = new HttpCookie("DeviceLog");

                cookie.Expires = DateTime.Now.AddDays(30);
                cookie.Values.Add("DeviceId", guid.ToString());
                Response.Cookies.Add(cookie);

                model.UserDevicesLogs.InsertOnSubmit(new UserDevicesLog {
                    Id = Guid.NewGuid(),
                    UserId = user.Id,
                    DeviceId = guid,
                    LastLoginFromDevice = DateTime.Now
                });
            } else {//Hvis device har en cookie
                Guid deviceId;
                Guid.TryParse(cookie.Values["DeviceId"], out deviceId);

                UserDevicesLog deviceLog = model.UserDevicesLogs.FirstOrDefault(x => x.UserId == user.Id && x.DeviceId == deviceId);

                if (deviceLog == null) {
                    model.UserDevicesLogs.InsertOnSubmit(new UserDevicesLog {
                        Id = Guid.NewGuid(),
                        DeviceId = deviceId,
                        UserId = user.Id,
                        LastLoginFromDevice = DateTime.Now
                    });
                } else {
                    deviceLog.LastLoginFromDevice = DateTime.Now;
                }
                Response.Cookies.Add(cookie);
            }

            model.SubmitChanges();

            FormsAuthentication.SetAuthCookie(username.Text, remember.Checked);
            if (String.IsNullOrEmpty(Request.QueryString["ReturnUrl"])) {
                Response.Redirect(STSUtility.GetRefereUrl(true));
            } else {
                FormsAuthentication.RedirectFromLoginPage(user.Email, remember.Checked);
            }

        }
    }

只有 DeviceLog cookie 给了我这个问题...

【问题讨论】:

  • 它在 Chrome 浏览器中运行良好,但是 Internet Explorer 和 FireFox 将 cookie 视为会话 cookie,而不是设置 30 天的过期日期。
  • 刚刚测试了你的代码,你的代码工作正常,我可以从IE读取cookie,我使用的是IE 11
  • 您想发布更多代码吗?例如页面加载、页面初始化和母版页中的一些代码等。
  • 是的,编辑了帖子
  • 您是否忘记在页面加载时添加 cookie..??

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


【解决方案1】:

好吧,我自己解决了这个问题,最后......

我有 Response.Cookies.Add(cookie);在代码中,这就是原因。不知道为什么这会是一个问题,因为它是两次添加的同一个 cookie,是的.. 愚蠢的愚蠢的愚蠢我知道 :) 感谢您的建议,我很高兴终于解决了这个问题 :)

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 2015-06-06
    • 2010-09-15
    • 2016-03-21
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多