gpt4 book ai didi

javascript - Internet Explorer 中的快速 session

转载 作者:IT老高 更新时间:2023-10-28 23:21:58 26 4
gpt4 key购买 nike

我这样配置 express-session 插件:

var express = require('express'),
session = require('express-session'),
uuid = require('node-uuid');

var expiration_day = new Date('9/15/2015'),
today = new Date(),
session_life = Math.abs(expiration_day.getTime() - today.getTime());

var config = {
name: 'mycookie', // session ID cookie name
key: 'mycookie', // session ID cookie name
secret: '$FGDFH$W#WEgfgdf',
hash: {
salt: '9883hjHFIDSU&U#H'
},
store: new MongoStore({mongooseConnection: db}),
unset: 'keep', //never remove expired sessions
saveUninitialized: true, //always create session, even if nothing is stored
resave: false, //don't save session if unmodified
ttl: session_life,
autoRemove: 'disabled', //disable expired sessions cleaning
genid: function (req) {
"use strict";
return uuid.v4();
},
cookie: {
path: '/api', // cookie will be stored for requests under '/api'
httpOnly: false,
domain: 'example.com',
secure: false,
expires: expiration_day,
maxAge: session_life
}
};

app.sessionMW = session(config);//session middleware

在 Chrome 和 Mozilla Firefox 浏览器中,只会为用户创建一个 session 。此 session 在使用 sessionMW 中间件的所有路由上都可用。因此,如果您向/api/users/或/api/sessions 发出 GET 或 POST 请求,相同的 session id 将存储在 cookie 中,并在每个请求的 cookie header 中发送。

Internet Explorer 无法以这种方式工作。对于每个请求,都会创建一个新 session 。 session 存储在服务器上,并且我已确认浏览器中针对应用程序的每个路由都有一个新的 cookie。

我已经在 cookie 中定义了域、路径和过期时间。IE 中的 cookie 显示了这些值。

我不使用cookieParser,所以这不是问题。

无论如何,问题似乎出在客户端。Internet Explorer 未随请求发送 Cookie header 。它在每个请求的响应中接收 set-cookie header 。但是这些数据永远不会在后续请求中重复使用。

这可能是 CORS 问题吗? cookie 不适用于我运行应用程序的同一个域。我需要一个关于托管在另一个域上的 API 的所有路由的 session 。

客户端配置为在 CORS 请求中包含 cookie:

$.ajaxSetup({
cache: false,
xhrFields: {
withCredentials: true //all AJAX requests should include Cookie data
}
});

我在对每个请求的响应中发送这些接受控制 header :

Access-Control-Allow-Credentials: true

Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type,

Accept Access-Control-Allow-Methods: OPTIONS, GET, POST, PUT, PATCH

Access-Control-Allow-Origin: http://example.com

为什么 IE 没有在请求中设置 Cookie header ?域的名称中没有下划线,也没有以数字开头。

最佳答案

对于那些使用 fetch 的人,请务必将凭据设置为 include

fetch('/url/', { credentials: 'include' });

否则 session ID 将不会包含在 IE Edge 中的请求中

关于javascript - Internet Explorer 中的快速 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31939758/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com