When using sub-domains for a Django site, how can you share django logins across sub-domains on localhost?

故事扮演 提交于 2019-12-07 03:24:15

问题


I want to let the same user session span across: site.com
sub1.site.com
sub2.site.com

How can I do this in Django? With the default auth user package it seems to require the user to login to all 3 sites each time with a different session. How can they share the same login cookie and session-id?

UPDATE: Using the SESSION_COOKIE_DOMAIN value in settings.py seems to work on production sites, but it doesn't work for me on localhost/dev servers. How do you get it to work for localhost sub-domains? When I change the SESSION_COOKIE_DOMAIN to the production website name or ".localhost" django auth logins completely stop working (I'm unable to ever login, no cookie is created on localhost.)


回答1:


I think I got a workaround solution, but couldn't use localhost. I could only get it working for a test ".com" domain that maps to 127.0.0.1.

In my /etc/hosts file (on OSX:)

    127.0.0.1  test.com
    127.0.0.1  sub1.test.com
    127.0.0.1  sub2.test.com

Then on my development settings.py:

    SESSION_COOKIE_DOMAIN=".test.com"

I could not get this working with plain "localhost", it seemed I needed the ".com" string in there to get it working. So then I could login and have cross subdomain auth cookies using sub1.test.com:8000 and sub2.test.com:8000 in my browser.



来源:https://stackoverflow.com/questions/1442081/when-using-sub-domains-for-a-django-site-how-can-you-share-django-logins-across

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!