【问题标题】:Cookie.Path doesn't work with Internet ExplorerCookie.Path 不适用于 Internet Explorer
【发布时间】:2012-03-14 10:27:51
【问题描述】:

我在登录期间创建 cookie。 cookie 范围应该被限制在路径中。

    HttpCookie cookie = new HttpCookie(cookieName);
    cookie.Expires = DateTime.Now.AddMinutes(expiryMinutes);
    cookie["username"] = username;
    SetCookieValue(cookie, "username", username);
    SetCookieValue(cookie, "password", password);
    SetCookieValue(cookie, "domain", domain);
    cookie.HttpOnly = true;
    cookie.Secure = true;
    cookie.Path = Request.Url.GetLeftPart(UriPartial.Authority);

问题在于,虽然这段代码在 Firefox 和 Chrome 中运行良好,但在 Internet Explorer(和 Opera)中却无法运行。有问题的行是最后一行 - 在此处设置 cookie.Path 参数时,IE 会丢弃(我认为)cookie。我已经使用 Chrome 控制台对此进行了调试,并且使用正确的路径(例如 /application )成功创建了 cookie。

这里可能有什么问题?

【问题讨论】:

    标签: asp.net iis-7 internet-explorer-9


    【解决方案1】:

    由于底层 WinINET InternetGetCookie 实现中的一个模糊错误,如果使用包含文件名的路径属性设置 IE 的 Document.Сookie,它将不会返回 cookie。

    例如,如果一个页面在自身上设置了一个 cookie,如下所示:

    Set-Cookie: HTTPSet-PathCookie=PASS;path=/check.htm
    

    cookie 将与 HTTP 请求一起发送,但不会出现在 Document.Сookiecollection 中。

    this blog entry

    【讨论】:

    • 嗯,很好。但我没有设置文件名,只有目录名,比如“/applicaion1”。而且还是不行(IE9)
    • 这里也一样,在 Chrome 和 Firefox 上工作得很好,在 IE(在 IE10 中测试)或 Opera 中根本不工作......
    【解决方案2】:

    改变

    cookie.Path = Request.Url.GetLeftPart(UriPartial.Authority);
    

    到这里

    cookie.Path = "/";
    

    我假设您想将 cookie 应用到整个域。

    【讨论】:

    • 不,我需要特定路径的 cookie。设置为“/”时有效,不用担心。将其设置为与 / 不同的内容时,问题就开始了。
    • 你能发布你的HTTP响应头吗?
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2015-07-01
    • 2013-07-05
    • 2012-01-27
    • 2015-12-04
    相关资源
    最近更新 更多