简体   繁体   English

flask session 在从 c# mvc Z2567A5EC93705EB7AC2CZ98 应用程序调用调用时不会持续存在

[英]flask session doesn't persist when invoke the call from c# mvc web application

I wrote a web service in python flask.我在 python flask 中写了一个 web 服务。 In my code I have flask session object and it looks good.在我的代码中,我有 flask session object 看起来不错。

Here is a short example how my python code look like-这是一个简短的示例,我的 python 代码看起来像 -

@app.route('/api/simpleTry', methods=['GET'])
def api_simple():
    user_id = request.args['id']
    sentence = request.args['sentence']

    if 'user_id' not in session.keys():
        session['user_id'] = user_id
    return jsonify("hello")
app.run()

when I invoke the service through 'crome' explorer it works good, the session persist and when I 2 open 2 clients (1- crome, 2-incognito) I can see that each one of them has a separate session.当我通过“crome”资源管理器调用服务时,它运行良好,session 持续存在,当我 2 打开 2 个客户端(1-crome,2-incognito)时,我可以看到它们每个都有一个单独的 session。

in addition, I have a C# mvc web application that act as my client and over there I call the python flask service I wrote.此外,我有一个 C# mvc web 应用程序作为我的客户端,在那里我调用 python Z319C3469116 服务编写。

address = "http://localhost:5000/api/simpleTry?id=" + userId + "&sentence=" + sentence;
using (var w = new WebClient())
{
     json_data = w.DownloadString(address);
     ...
}

The code works well.代码运行良好。 but , as opposed to 'crome', when sending the request from c#, the session in python does not persist and create a new session for each request. but , as opposed to 'crome', when sending the request from c#, the session in python does not persist and create a new session for each request.

I guess I should send something from C# to the service in order to use the existing session, but I don't what and how.我想我应该从 C# 向服务发送一些东西,以便使用现有的 session,但我不知道什么和如何。

Flask sessions uses cookies to communicate session information between the client & server. Flask 会话使用 cookies 在客户端和服务器之间传递 session 信息。 When you used a browser, the browser is the client & automatically sends the cookies back to the server.当您使用浏览器时,浏览器是客户端并自动将 cookies 发送回服务器。 When you use a C# client, I'm not sure if it does that for you automatically.当您使用 C# 客户端时,我不确定它是否会自动为您执行此操作。 You may have to read the session cookie from the header & send it along with subsequent requests.您可能必须从 header 中读取session cookie 并将其与后续请求一起发送。

From the Flask documentation : This is implemented on top of cookies for you and signs the cookies cryptographically来自Flask 文档This is implemented on top of cookies for you and signs the cookies cryptographically

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM