Rails:如何在多个子域中共享永久Cookie? [英] Rails: how can I share permanent cookies across multiple subdomains?

查看:101
本文介绍了Rails:如何在多个子域中共享永久Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式的注册和登入程序是在安全的子网域上进行。因此,我修改了 config / initializers / session_store.rb 看起来像

 如果Rails.env.production? 
AppName :: Application.config.session_store:cookie_store,:key => '_app_name_session',:domain => '.app_name.com'
else
AppName :: Application.config.session_store:cookie_store,:key => '_app_name_session'
end

,以便可以跨子域共享会话。 p>

如何通过子域名共享永久Cookie,这样当我通过 cookies.permanent [:some_key]在一个子域上设置永久cookie
时, ='some value'
,我可以通过 cookies [:some_key] 访问另一个子域上的cookie?

解决方案

您需要使用更详细的Cookie设置方法指定域:

  cookie [:some_cookie] = {
:value => whatever,
:domain => .app_name.com,
:expires => 1.year.from_now.utc
}

我没有找到配置设置在全球范围内这样做。


My app's register and login processes take place on a secure subdomain. For this reason, I have modified config/initializers/session_store.rb to look like

if Rails.env.production?
  AppName::Application.config.session_store :cookie_store, :key => '_app_name_session', :domain => '.app_name.com'
else
  AppName::Application.config.session_store :cookie_store, :key => '_app_name_session'
end

so that the session can be shared across sub domains.

How can I share permanent cookies accross subdomains so that when I set a permanent cookie on one subdomain via cookies.permanent[:some_key] = 'some value', I can access that cookie on another subdomain via cookies[:some_key]?

解决方案

You need to specify the domain using a more verbose cookie setting method:

cookies[:some_cookie] = {
  :value => "whatever",
  :domain => ".app_name.com",
  :expires => 1.year.from_now.utc
}

I haven't found a configuration setting to do this globally yet.

这篇关于Rails:如何在多个子域中共享永久Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆