在 302 重定向期间发送浏览器 cookie
在 302 重定向期间发回 cookie 是否存在任何问题?例如,如果我创建一个 return-to-url cookie 并在同一响应中重定向用户,任何(现代)浏览器都会忽略该 cookie 吗?
Are there any issues with sending back a cookie during a 302 redirect? For example, if I create a return-to-url cookie and redirect the user in the same response will any (modern) browser ignore the cookie?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
根据这篇博客文章: http://blog.dubbelboer.com/2012 /11/25/302-cookie.html 所有主流浏览器,IE (6, 7, 8, 9, 10), FF (17), Safari (6.0.2), Opera (12.11) 均在 Windows 上和 Mac,在重定向上设置 cookie。对于 301 和 302 重定向都是如此。
正如 @Benni 指出的:
https://www.chromium。 org/administrators/policy-list-3/cookie-legacy-samesite-policies
According to this blog post: http://blog.dubbelboer.com/2012/11/25/302-cookie.html all major browsers, IE (6, 7, 8, 9, 10), FF (17), Safari (6.0.2), Opera (12.11) both on Windows and Mac, set cookies on redirects. This is true for both 301 and 302 redirects.
As @Benni noted :
https://www.chromium.org/administrators/policy-list-3/cookie-legacy-samesite-policies
一个通知(以挽救开发人员的生命):
当 cookie 的域为 localhost 时,IE 和 Edge 会忽略重定向响应中的 Set-Cookie。
解决方案:
使用127.0.0.1而不是localhost。
One notice (to save developer's life):
IE and Edge are ignoring Set-Cookie in redirect response when domain of the cookie is localhost.
Solution:
Use 127.0.0.1 instead of localhost.
大多数浏览器都接受 302 重定向的 cookie。我对此非常确定,但我做了一些搜索。并非所有现代浏览器。
来自现已删除/失效的 Internet 存档链接 microsoft connect Silverlight 客户端 HTTP 堆栈上的 Q/A 忽略 Set-Cookie 302 重定向响应 (2010)
我认为我们现在有了 IE6 的替代品,它就是 Windows Mobile 浏览器...
Most browser are accepting cookies on 302 redirects. I was quite sure of that, but I made a little search. Not all modern browsers.
Internet archive Link from a now removed/dead/ microsoft connect Q/A on Silverlight Client HTTP Stack ignores Set-Cookie on 302 Redirect Responses (2010)
I think we now have a replacement for IE6 and it's Windows Mobile browsers...
我们最近(2022 年 3 月)遇到了这个问题 - Firefox 和 Chrome 都没有立即在 HTTP 302 重定向上设置 cookie。
详细信息:
我们必须通过客户端重定向来提供 HTTP 200 来解决这个问题:
We hit this issue recently (Mar 2022) - both Firefox and Chrome didn't set the cookies immediately on HTTP 302 redirect.
Details:
We had to work around this by serving HTTP 200 with a client-side redirect instead:
这里是此问题的 Chromium 错误(Set-cookie 被忽略)对于状态为 302 的 HTTP 响应)。
Here is the Chromium bug for this issue (Set-cookie ignored for HTTP response with status 302).
我刚刚在 Firefox 和 Safari 上都遇到了这个问题,但 Chrome 没有。根据我的测试,只有当域在重定向期间发生更改时才会发生这种情况。这是 OAuth2 流程中的典型情况:
由于我尚未弄清楚的原因,请求 2 中的一些 cookie 被忽略,而其他 cookie 则未被忽略。但是,如果请求 2 返回带有
Refresh
标头(“元刷新”重定向)的 HTTP 200,则请求 3 会正确设置 cookie。I just ran into this problem with both Firefox and Safari, but not Chrome. From my testing, this only happens when the domain changes during the redirect. This is typical in an OAuth2 flow:
For reasons I haven't figured out yet, some cookies from request 2 are ignored while others are not. However, if request 2 returns a HTTP 200 with a
Refresh
header (the "meta refresh" redirect), cookies are set properly by request 3.这是一种非常不受欢迎的方法,但如果您确实不想依赖 30x set-cookie 浏览器行为,您可以在设置 cookie 时使用 HTML
meta http-equiv="refresh"
“redirect” 。例如,在 PHP 中:服务器将发送带有 200 而不是正确的 300x 重定向的 Set-Cookie,因此浏览器将存储 cookie,然后执行“重定向”。
链接是浏览器不执行元刷新时的后备链接。
This is a really frowned upon approach, but if you really want to not rely on 30x set-cookie browser behavior you could use an HTML
meta http-equiv="refresh"
"redirect" when setting the cookie. For example, in PHP:Server will send Set-Cookie with a 200 instead of a proper 300x redirect, so browser will store the cookie, and then perform the "redirect". The
<a>
link is a fallback in case browser does not perform the meta refresh.在 .Net 上使用 OpenIdConnect / IdentityServer 时遇到此问题,其中单独的 API(不同的主机名)处理身份验证并重定向回主站点。
首先(对于本地主机上的开发)您需要将 CookieSecure 选项设置为 SameAsRequest 或 Never 来处理 http://localhost/ 不安全。请参阅 Michael Freidgeim 的回答。
其次,您需要将 CookieSameSite 属性设置为 Lax,否则 cookie 根本不会保存。
严格
在这里不起作用!Encountered this issue while using OpenIdConnect / IdentityServer on .Net, where a separate API (different hostname) handles authentication and redirects back to the main site.
First (for development on localhost) you need to set
CookieSecure
option toSameAsRequest
orNever
to deal withhttp://localhost/
not being secure. See Michael Freidgeim's answer.Second, you need to set the
CookieSameSite
attribute toLax
, otherwise the cookies do not get saved at all.Strict
does not work here!就我而言,我设置 CookieOptions.Secure=true,但在 http://localhost 上进行了测试,并且浏览器根据以下内容隐藏 cookie设置。
为了避免此类问题,您可以使cookie Secure选项匹配协议Request.IsHttps,例如
In my case I set CookieOptions.Secure=true, but tested it on http://localhost., and browser hide cookies according to the setting.
To avoid such problem, you can make cookie Secure option to match protocol Request.IsHttps,e.g.