Cookie未保存在浏览器中 [英] Cookies not saved in the browser

查看:426
本文介绍了Cookie未保存在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Python Flask后端在浏览器中设置cookie,但是,当我们调用set cookie函数时,我无法观察到浏览器正在保存的cookie。以下是我目前对该工作方式的理解:

I am trying to set a cookie in my browser using a Python Flask backend however, when we call the set cookie function I am unable to observe the cookie being saved by the browser. The following is my current understanding about how this is supposed to work:


  • Cookie只是可能会过期的键值对持久的,否则它们会在关闭浏览器时过期

  • 以设置Cookie,所需要做的就是在响应中使用set-cookie标头。我调用烧瓶响应对象的set_cookie方法来执行此操作。

  • 浏览器应自动保存cookie并遵循到期规则(在浏览器收到的响应中可以看到set_cookie标头)

在Angular HttpClient中进行请求

Making the request in Angular HttpClient

let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers, withCredentials: true });
const request_data = {'username': this.username, 'password': this.password};
this.http.post('http://localhost:8080/token', request_data, options)

在Python Flask中设置cookie

Setting the cookie in Python Flask

g.response = make_response()
time = datetime.datetime.now() + datetime.timedelta(days=30)
g.response.set_cookie("auth_token", auth.token, expires=time)
return g.response

浏览器中的纯文本响应

HTTP/1.1 200 OK
set-cookie: auth_token=7253f2fa43d7584741dcf8972dea8f; Expires=Fri, 05-Jan-2018 01:33:30 GMT; Path=/
vary: Origin
access-control-allow-credentials: true
access-control-allow-origin: http://127.0.0.1:4200
content-type: application/json
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache
Content-Length: 58
Server: Development/2.0
Date: Wed, 06 Dec 2017 01:33:30 GMT

浏览器的Cookie部分

Browser's cookies section

其他想法和行为;探索的帖子:

Other thoughts & posts explored:

  • Tried using both Safari and Chrome, and received the same result in both. I have also verified that cookies are allowed by the browser.
  • $http doesn't send cookie in Requests
  • Cookie is not set in browser
  • How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?

问题:

我如何获取浏览器保存的cookie,以便可以在浏览器中使用

How do I get the cookies to be saved by the browser so that it can be used in the current session?

推荐答案

cookie的域设置为回送地址(127.0.0.1)。在角度上,我使用的是 localhost而不是环回地址来调用set-cookie端点,这阻止了将cookie保存在浏览器中。一旦Cookie域,端点基础URL和浏览器地址使用回送地址匹配,一切就按预期工作。

The domain for the cookie was set to the loopback address (127.0.0.1). In angular, I was calling the set-cookie endpoint using 'localhost' instead of the loopback address which prevented the cookies to be saved in the browser. As soon as cookie domain, endpoint base URL, and browser address matched using the loopback address, everything worked as expected.

有趣的旁注:目前尚不确定为什么,但是匹配的地址似乎还不够。我还尝试将Cookie域,端点基本URL和浏览器地址都设置为 localhost,但这仍然没有设置Cookie。仅当所有值都是回送地址时,它才起作用。

Interesting side note: I am not sure why at the moment, but matching addresses doesn't seem to enough. I also tried setting both the cookie domain, endpoint base URL, and browser address to 'localhost' but this still didn't set the cookie. It only worked once all values were the loopback address.

这篇关于Cookie未保存在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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