如何解决Set-Cookie在Chrome和Dolphin中不起作用-有两个网站
请查看8个月前的this question and answer。答案暂时解决了该问题,但是今天我发现在Chrome和Dolphin中,登录和注销对于我的每个网站(域)都分别可以再次使用。 但是,一切在Firefox,Edge和Opera中都可以像以前一样工作。这些浏览器中是否存在其他域名Cookie的更改,以及如何解决该问题,以便登录和注销在两个网站上都可以同时进行?
用户登录或注销或注册一个网站,我也希望他们也自动从另一网站登录或注销,并且可以与Firefox,Edge和Opera一起使用。但是Chrome和Dolphin的用户当前(如果他们登录或注销到一个网站)不会影响另一个网站。
Django视图代码为:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getSheetId());
JavaScript代码为:
@csrf_exempt
def set_session(request):
"""
Cross-domain authentication.
"""
response = HttpResponse('')
origin = request.META.get('HTTP_ORIGIN')
if isinstance(origin,bytes):
origin = origin.decode()
netloc = urlparse(origin).netloc
if isinstance(netloc,bytes):
netloc = netloc.decode()
valid_origin = any(netloc.endswith('.' + site.domain) for site in Site.objects.all().order_by("pk"))
if (not (valid_origin)):
return response
if (request.method == 'POST'):
session_key = request.POST.get('key')
SessionStore = import_module(django_settings.SESSION_ENGINE).SessionStore
if ((session_key) and (SessionStore().exists(session_key))):
# Set session cookie
request.session = SessionStore(session_key)
request.session.modified = True
else:
# Delete session cookie
request.session.flush()
response['Access-Control-Allow-Origin'] = origin
response['Access-Control-Allow-Credentials'] = 'true'
return response
然后有一个JavaScript代码两次调用此函数:
window.speedy = {};
window.speedy.setSession = function (domain,key) {
$.ajax({
url: '//' + domain + '/set-session/',method: 'post',data: {
key: key
},xhrFields: {
withCredentials: true
}
});
};
speedy.setSession('speedy.net','session_key');
speedy.setSession('speedymatch.com','session_key');
替换为用户的会话密钥的地方。
和Django设置(对于Django 3.0.6):
'session_key'
这个问题有解决方案吗?我认为这是由于Chrome和Dolphin浏览器最近发生的变化。
我检查了一下,并从控制台收到以下错误:
与以下链接相关:
解决方法
设置了Cookie ...,但未设置“ SameSite”属性。
从2020年7月14日开始,您应该设置以下 1,2 并升级到Django 3.1 3,4 (8月4日发布):
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = 'None'
说明
在Django 3.1之前,如果设置为samesite
单例,则未设置None
属性:
if samesite:
if samesite.lower() not in ('lax','strict'):
raise ValueError('samesite must be "lax" or "strict".')
self.cookies[key]['samesite'] = samesite
从Django 3.1开始,为samesite
字符串设置了'None'
属性;仍然不适用于None
/ False
:
if samesite:
if samesite.lower() not in ('lax','none','strict'):
raise ValueError('samesite must be "lax","none",or "strict".')
self.cookies[key]['samesite'] = samesite
参考
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。