如何读取不是由Flask设置的cookie [英] How to read cookies not set by Flask

查看:200
本文介绍了如何读取不是由Flask设置的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flask站点,在一个WordPress站点的iframe中运行。

这两个网站都在同一个域中。也就是说,WordPress的网站是类似于www.example.com和Flask网站是在api.example.com。

我需要显示一些用户特定的东西iframe。那么我怎么读一个在Flask中由wordpress设置的cookie呢?或检查是否设置?
基本上,我想知道用户是否登录(会话cookie存在)和他们的用户名。

最好是如果我能以某种方式将Flask-Login插件与实际的Wordpress登录集成在一起,然而,我不认为这是可能的。

解决方案

当一个cookie被创建时,它的域被设置。您需要确保域名已设置,以便两个网站域名匹配 Cookie域。例如 example.com 将允许这两个站点查看cookies,或者以另一种方式将它们与对两个服务器的请求一起设置。



wordpress登录可以与Flask集成。您将需要提取会话ID / cookie内容,然后向wordpress网站发出请求。

烧瓶代码:

  request.cookies.get('username ')#get single cookie 
request.cookies#get all as dict

Cookie属性



<这个请求检查会话是否有效并且返回用户证书。
如果成功 - 表示用户可以自动登录 - 然后调用

有关 cookies


I have a Flask site that runs inside an iframe of a wordpress site.

Both sites are on the same domain. That is, the wordpress site is on something like www.example.com and Flask site is on api.example.com.

I need to show some user specific stuff in the iframe. So how do I read a cookie that was set by wordpress in Flask? Or check if it is set? Basically, I want to know if the user is logged in or not (session cookie present) and their username.

The best would be if I could somehow integrate the Flask-Login plugin with the actual Wordpress login, however, I do not expect that to be possible.

解决方案

When a cookie is created its domain is set. You need to ensure that the domain is set so that both sites domain match the cookie domain. For example example.com will allow the cookies to be viewed by both sites or put another way they will be set along with the request to both servers.

The wordpress login can be integrated with Flask. You will need to extract the session id/cookie contents and then make a request to the wordpress site.

Flask Code:

request.cookies.get('username')  # get single cookie
request.cookies  # get all as dict

Cookie attribute

This requests check if the session is valid and returns the user credentials. If it succeeds - meaning the user can be automatically logged in - you then invoke cookies

Note: You will have to synchronize the sessions manually. When a session is created on the api side it will continue to exist until it expires or you manually call logout. Also if you log out of the wordpress side the flask session will continue to exist

More info on cookies

这篇关于如何读取不是由Flask设置的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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