簡體   English   中英

Internet Explorer中的快速會話

[英]Express-session in Internet Explorer

我像這樣配置快速會話插件:

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瀏覽器中,只為該用戶創建了一個會話。 此會話適用於使用sessionMW中間件的所有路由。 因此,如果您對/ api / users /或/ api / sessions執行GET或POST請求,則相同的會話ID將存儲在cookie中,並在每個請求的cookie頭中發送。

Internet Explorer不能以這種方式工作。 對於每個請求,都會創建一個新會話。 會話存儲在服務器上,我已經確認瀏覽器中有一個新的cookie用於應用程序的每個路由。

我已經定義了cookie中的域,路徑和過期。 IE中的cookie顯示了這些值。

我不使用cookieParser,所以這不是問題。

無論如何,問題似乎是在客戶端。 Internet Explorer未發送帶有請求的Cookie標頭。 它在每個請求的響應中接收set-cookie標頭。 但是數據永遠不會在后續請求中重復使用。

這可能是一個CORS問題嗎? 該cookie不適用於我運行該應用程序的同一域。 我需要在另一個域上托管的API的所有路由上進行會話。

客戶端配置為在CORS請求中包含cookie:

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

我在響應每個請求時發送這些accept-control標頭:

Access-Control-Allow-Credentials:true

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

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

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

為什么IE沒有在請求中設置Cookie標頭? 域名的名稱中沒有下划線,並且不以數字開頭。

確保您的服務器時區和時間正確無誤。 我剛剛調試了服務器的時間是本地但時區設置為UTC的問題。 不知何故,Chrome和Firefox仍然正確地解釋了過期,但IE尊重UTC時區,因此cookie已經過期了。 請注意,如果您處於與UTC相關的負時區,它將立即過期。

對於那些使用fetch人,請務必將憑據設置為include

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

否則,會話ID將不會包含在IE Edge中的請求中

在您的域名中加下划線也可能導致此問題。

IE和Edge不存儲域名的cookie,包括下划線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM