【发布时间】:2018-05-28 14:42:37
【问题描述】:
我目前正在开发一个使用 http 请求并使用 cookie 进行身份验证的 ionic 应用程序。
首先,我登录:
login(login, password): Observable<number>{
let body = new FormData();
body.append("pseudo", login);
body.append("password", password);
let url = this.baseUrl+"/service/login";
return Observable.create(observer => {
this.http.post(url, body, this.options)
.map(res => res.json())
.subscribe(
data => {
// code omitted
},
err => {
// code omitted
}
);
});
}
我在 set-cookie 响应标头中收到 cookie:
access-control-allow-credentials:true
access-control-allow-origin:http://127.0.0.1:8100
access-control-expose-headers:WWW-Authenticate,Server-Authorization
cache-control:no-cache
Connection:keep-alive
content-encoding:gzip
content-type:text/html; charset=utf-8
Date:Thu, 14 Dec 2017 13:02:15 GMT
set-cookie:multipass-cookie=Fe26.2**99636c19e52bde1e7ed632373ddfc5ee505aa19377c39aa40b7982d97fbcb023*6QYsXbrTmi5LoYMxngrPzA*y5Z5oz9Tlq-Gzn9yj9yZ-ue5hDWxt1D7XV_nnohdyTA**e38b047dddebd4a83975d883917f375de87673c0542e8f774d5034a5337000b9*cRK9uIlDVDnYoJ0lwwRTy-7hBWEhWZQojw91XPHj3-U; HttpOnly; SameSite=Strict; Path=/
Transfer-Encoding:chunked
vary:origin,accept-encoding
然后我再拨打一个需要 cookie 的电话:
getUserInfos(): Observable<number>{
let url = this.baseUrl+"/service/playerInfo";
return Observable.create(observer => {
this.http.get(url, this.options)
.map(res => res.json())
.subscribe(
data => {
// code omitted
},
err => {
// code omitted
}
);
});
}
并且 cookie 不会在请求标头中发送。 Chrome 会发送我没有要求的其他 cookie(PHPSESSID 和 i18next):
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6
Cache-Control:no-cache
Connection:keep-alive
Cookie:PHPSESSID=hmkgpm6hb10m9ldi7nm5vqnjq0; i18next=fr-FR
Host:www.jeuxpresto.com:2773
Origin:http://127.0.0.1:8100
Pragma:no-cache
Referer:http://127.0.0.1:8100/?ionicplatform=ios&ionicstatusbarpadding=true&http://127.0.0.1:8100/ionic-lab
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
我使用 withCredentials:true 选项,它在 this.options 中设置,构造如下:
this.headers = new Headers();
this.options = new RequestOptions({ headers: this.headers, withCredentials: true });
在 Firefox 上运行良好,发送了正确的 cookie,一切正常。 可能是什么问题?
非常感谢!
【问题讨论】:
标签: angular google-chrome http cookies ionic-framework