IE 中的 Cookie 异常

发布于 2025-01-08 10:11:56 字数 2459 浏览 0 评论 0原文

我有一些代码将用户的 ID 保存为 cookie。它在生产中工作得很好,但是将代码移至 IIS7、升级代码后面的供应商应用程序以及将应用程序移至 IIS7 中的应用程序而不是仅运行 Default Web 会破坏 IE 中的此 cookie 功能。

不幸的是,它是一个经典的 ASP 应用程序,所以我找不到发布工作版本的好方法。但这里是相关的部分。

概要:

  1. 当用户勾选“记住我”并登录时,一个临时cookie 创建
  2. 当用户进行身份验证时 ,临时 cookie 被“提升”为 当用户取消选中“记住我”时,永久 cookie 和临时 cookie 都会过期,
  3. 这两个 cookie 都应该是 已过期

似乎发生的情况(仅在 IE 中?)是有 2 个 cookie,取消选中该框只会触及其中一个。

这是相关代码。希望它有所帮助:)

在登录表单上:

var MHOLI = Get_Cookie("MHOLI");
//Check if cookie has a value
if (MHOLI != null && MHOLI != "" && MHOLI != "null") {
    //Set login text
    $("#Login").val(MHOLI);
    //keep remember login checkbox checked
    $("#RemonlineID").attr('checked', true);
    $(document).ready(function() {
        setTimeout(function() {
            $("#Password").focus();
        }, 200);
    });
}
$(document).ready(function() {
    //test if cookies are enabled..
    Set_Cookie('test', 'testvalue', '/', '', '');
    //if cookies are disabled, disable the option to remember username
    if (!Get_Cookie('test')) {
        $('#RemonlineID').attr("disabled", true);
    }
});​

当“记住我”复选框更改时:

var loginForm = document.getElementById("loginForm");
if (!loginForm.RemonlineID.checked) {
    setCookie("MHOLI", null, null);
    setCookie("tmpMHOLI", null, null);
}​

提交登录表单时,如果选中“记住我”,则设置 1 天的 cookie:

if (loginForm.RemonlineID.checked) {
    setCookie("tmpMHOLI", loginForm.Login.value, 1);
}
else {
    setCookie("tmpMHOLI", null, null);
}​

setCookie 函数。是的,我看到 expstring 在那里,但从未使用过:):

function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = (typeof days != "undefined") ? expireDate.setDate(expireDate.getDate() + parseInt(days)) : expireDate.setDate(expireDate.getDate() - 5)
    document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString();
}​

然后,一旦用户将其放入应用程序,就会出现一些 VBScript。我认为它正在创建第二个 cookie,而不是

if Request.Cookies("tmpMHOLI") <> "" then
  Response.Cookies("MHOLI") = Request.Cookies("tmpMHOLI")
  Response.Cookies("MHOLI").Expires = Date() + 365
  Response.Cookies("tmpMHOLI") = ""
end if​

IE7/8/9 处理 cookie 的方式是否有不同,这不起作用? IIS7.5 是否有某些东西正在创建客户端脚本无法触及的 cookie?

I have some code that saves the user's ID as a cookie. It works fine in production, but moving the code to IIS7, upgrading the vendor app behind my code, and moving the app to an app in IIS7 instead of just running Default Web breaks this cookie function in IE.

Unfortunately it's a Classic ASP app so I can't find a good way to post a working version. But here are the relevant pieces.

Synopsis:

  1. when the user checks "remember me" and logs in, a temporary cookie
    is created
  2. when the user authenticates, the temp cookie is "promoted" to a
    permanent one and temp is expired
  3. when the user unchecks "remember me" both cookies are supposed to be
    expired

What appears to be happening (just in IE?) is that there are 2 cookies, and unchecking the box only touches one of them.

Here is the relevant code. Hope it helps :)

On the login form:

var MHOLI = Get_Cookie("MHOLI");
//Check if cookie has a value
if (MHOLI != null && MHOLI != "" && MHOLI != "null") {
    //Set login text
    $("#Login").val(MHOLI);
    //keep remember login checkbox checked
    $("#RemonlineID").attr('checked', true);
    $(document).ready(function() {
        setTimeout(function() {
            $("#Password").focus();
        }, 200);
    });
}
$(document).ready(function() {
    //test if cookies are enabled..
    Set_Cookie('test', 'testvalue', '/', '', '');
    //if cookies are disabled, disable the option to remember username
    if (!Get_Cookie('test')) {
        $('#RemonlineID').attr("disabled", true);
    }
});​

When the "remember me" checkbox is changed:

var loginForm = document.getElementById("loginForm");
if (!loginForm.RemonlineID.checked) {
    setCookie("MHOLI", null, null);
    setCookie("tmpMHOLI", null, null);
}​

When the login form is submitted, set a 1 day cookie if "remember me" checked:

if (loginForm.RemonlineID.checked) {
    setCookie("tmpMHOLI", loginForm.Login.value, 1);
}
else {
    setCookie("tmpMHOLI", null, null);
}​

The setCookie function. Yes, I see that expstring is there but never used :):

function setCookie(name, value, days) {
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = (typeof days != "undefined") ? expireDate.setDate(expireDate.getDate() + parseInt(days)) : expireDate.setDate(expireDate.getDate() - 5)
    document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString();
}​

And then some VBScript once the user makes it into the application. I'm thinking that it is creating a second cookie instead of

if Request.Cookies("tmpMHOLI") <> "" then
  Response.Cookies("MHOLI") = Request.Cookies("tmpMHOLI")
  Response.Cookies("MHOLI").Expires = Date() + 365
  Response.Cookies("tmpMHOLI") = ""
end if​

Is there something different about how IE7/8/9 handle cookies that this would not work? Is there something about IIS7.5 that is creating a cookie that the client script can't touch?

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

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

发布评论

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

评论(2

无远思近则忧 2025-01-15 10:11:56

显然,域名中的 IE / IIS7 和下划线有些奇怪。这会影响你吗?

Apparently, there's some weirdness with IE / IIS7 and underscores in domain names. Could this be affecting you?

我还不会笑 2025-01-15 10:11:56

我最终重构了我的 setCookie() 函数。我没有正确地让cookie过期,因为日期计算很奇怪。 quirksmode createCookie() 函数工作正常。

另外,我在服务器端设置 cookie 时设置了路径。不知何故,登录前和登录后页面设置的 cookie 路径不同。因此客户端脚本无法覆盖服务器端 cookie,反之亦然。显式设置路径解决了这个问题。

I ended up refactoring my setCookie() function. I was not properly expiring the cookies because the date calculations were funky. The quirksmode createCookie() function worked correctly.

Also, I set the path on the cookie when I set it server-side. Somehow the paths were different for cookies set by the prelogin and post login pages. So then the client script couldnt override the server side cookie and vice versa. Explicitly setting the path fixed that.

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