gpt4 book ai didi

ruby-on-rails - 删除 Rails 3 子域 cookie

转载 作者:行者123 更新时间:2023-12-05 06:45:07 24 4
gpt4 key购买 nike

我遇到了一些用户从我的域中获得多个 session cookie 的问题。

在 session_store 中,我们最初使用 domain::all

因此,用户有时会得到域 example.comwww.example.com

的 cookie

对于外部站点,特定于子域​​的 cookie 无关紧要。我只想为整个网站使用一个 cookie。

如果我只是更改 session_store 以添加 domain: all,这并不一定意味着现有的 cookie 会消失。事实上,用户现在可能会有第三个 cookie,现在有一个用于 example.comwww.example.com.example.com

考虑到 cookie RFC 的编写方式以及 Rack 如何实现处理 cookie,当用户发出请求时,可能会加载错误的旧 cookie。 It just loads up the 'first' one in the cookie string from the header.

所以...有谁知道从用户浏览器中删除旧 cookie 的可靠方法。具体来说,我想删除域 www.example.comexample.com 中的 cookie,同时将 .example.com 中的 cookie 保留在-机智。

谢谢。

最佳答案

如果你需要防止其他domain::all cookies完好无损,我相信最简单的方法是直接从服务器添加一个Set-Cookie header来使它过期.

# When you believe the client is sending a cookie with `Domain=www.example.com` or `Domain=example.com`, instead of `Domain=.example.com`
response.set_header('Set-Cookie', '_my_app_session=expiring; Path=/; Max-Age=0; HttpOnly')

# For Rails < 5 (not tested)
headers['Set-Cookie'] = '_my_app_session=expiring; Path=/; Max-Age=0; HttpOnly'

我的其他尝试:

  1. reset_sesion 将不起作用,因为它将根据 domain::all 设置使用 Domain=.example.com
  2. 我也想从客户端使它过期,但是 HttpOnly 属性(rails 的默认值)阻止 js 操纵这些 cookie。
  3. 等待“ session ”cookie 过期的时间可能比您想象的要长,因为大多数浏览器在重启后实际上不会过期 (ref)

关于ruby-on-rails - 删除 Rails 3 子域 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com