我一定是忽略了饼干的一些基本特性。在localhost上,当我在服务器端设置cookie并显式地将域指定为localhost(或.localhost)时。有些浏览器似乎不接受cookie。

Firefox 3.5:我在Firebug中检查了HTTP请求。我看到的是:

Set-Cookie:
    name=value;
    domain=localhost;
    expires=Thu, 16-Jul-2009 21:25:05 GMT;
    path=/

或者(当我设置域为.localhost时):

Set-Cookie:
    name=value;
    domain=.localhost;
    expires=Thu, 16-Jul-2009 21:25:05 GMT;
    path=/

在这两种情况下,cookie都不会被存储。

IE8:我没有使用任何额外的工具,但cookie似乎没有被存储,因为它没有在后续的请求中被发送回来。

Opera 9.64: localhost和.localhost都可以工作,但是当我检查Preferences中的cookie列表时,域被设置为localhost。Local,即使它列在localhost下(在列表分组中)。

Safari 4: localhost和.localhost都可以工作,但它们总是在首选项中列出为.localhost。另一方面,一个没有显式域的cookie,它只显示为localhost(没有点)。

localhost有什么问题?由于存在如此多的不一致,必须有一些涉及localhost的特殊规则。另外,我也不完全清楚为什么域名必须以一个点作为前缀?RFC 2109明确指出:

Domain属性的值 不包含或不包含嵌入点 从一个点开始。

为什么?该文档表明它必须在安全性方面做一些事情。我必须承认我没有阅读完整的规范(可能以后会读),但它听起来有点奇怪。基于此,在本地主机上设置cookie是不可能的。


当前回答

Cookie需要指定SameSite属性,None值曾经是默认值,但最近的浏览器版本将Lax作为默认值,以对某些类型的跨站请求伪造(CSRF)攻击具有相当强大的防御能力。

除了SameSite=Lax,你还应该有Domain=localhost,这样你的cookie就会关联到localhost并被保存。它应该看起来像这样:

document.cookie = `${name}=${value}${expires}; Path=/; Domain=localhost; SameSite=Lax`;

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite

其他回答

我解决的跨站点cookie问题是这样的:

后端

服务器端

服务网站:http://localhost:8080 创建响应时,设置Cookie

属性:

SameSite=None; Secure; Path=/

客户端

Frontend(在我的例子中是Angular)

服务网站:http://localhost:4200/ 当发送请求到服务器(后端)

设置XHR.withCredentials = true:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8080/', true);
xhr.withCredentials = true;
xhr.send(null);

我的解释:

when backend and frontend domains differ the decision if the cookies will be saved in frontend domain cookie storage from received response is brought by the browser. Browser will allow sending cookies ONLY if XHR request has withCredentials=true and correct server Cookie attributes (HTTP Set-Cookie header) are recieved when backend and frontend domains differ the decision if the cookies will be sent within request is brought by the browser. Browser will allow this ONLY if XHR request has withCredentials=true in other words, if withCredentials=true is ommited - cookies won't be sent within request NOR will be recieved and saved from response recieved cookies are allways stored under frontend domain name in browser cookie storage. In case when server domain differs and cookies are saved successfully, the effect is the same as if they have been sent by frontend domain in the first place. if SameSite=None cookie attribute is omitted today's browser (Firefox/Chrome) will use default Lax mode which is too strict for cross site cookies if Secured cookie attribute is ommited - then SameSite=None will be ignored - it requires Secured to be set for localhost Secured cookie property browser does not require HTTPS / SSL, http will work - no need to serve frontend or backend under https://localhost ...

编辑2022-03-02 -对于Safari (v15.1),这是不正确的->在Safari http://localhost + cookie与安全- cookie将被忽略,而不是保存在浏览器中(解决方案:对于Safari + http://localhost删除Secure和SameSite如果提供)。

编辑2023-01-13 - @Barnaby报告说“Firefox拒绝设置它:'被拒绝,因为非https cookie不能被设置为'安全'。’”如果是这样的话,针对Safari的解决方案应该可以工作(见上面的EDIT 2022-03-02)。

诊断提示:

为了检查cookie是否被发送-打开浏览器开发工具并检查网络选项卡。找到后端请求并检查Headers -在请求头中搜索Cookie头,在响应头中搜索Set-Cookie 为了检查cookie是否被保存-打开浏览器开发工具,查看存储管理器(Firefox),检查cookie并搜索前端域名,检查cookie是否存在,如果存在,检查它是什么时候创建的… 别忘了先在后端设置CORS

参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

你可以使用localhost.org或。localhost.org,它总是会解析为127.0.0.1

我自己花了很多时间来解决这个问题。

使用PHP,这个页面上的任何东西都不适合我。我最终在我的代码中意识到,PHP的session_set_cookie_params()的“安全”参数总是被设置为TRUE。

因为我没有用https访问localhost,我的浏览器永远不会接受cookie。所以,我修改了我的代码的那一部分,有条件地设置'安全'参数基于$_SERVER['HTTP_HOST']是'localhost'或不是。现在工作得很好。

我希望这能帮助到一些人。

所有建议的修复程序都不适合我——设置为null、false、添加两个点等等——都不起作用。

最后,我只是从cookie中删除了域,如果它是localhost,现在在Chrome 38中为我工作。

以前的代码(不工作):

document.cookie = encodeURI(key) + '=' + encodeURI(value) + ';domain=.' + document.domain + ';path=/;';

新代码(现在工作):

 if(document.domain === 'localhost') {
        document.cookie = encodeURI(key) + '=' + encodeURI(value) + ';path=/;' ;
    } else {
        document.cookie = encodeURI(key) + '=' + encodeURI(value) + ';domain=.' + document.domain + ';path=/;';
    }

我在使用127.0.0.1作为域进行本地测试时运气要好得多。我不知道为什么,但我有混合的结果与localhost和.localhost等。