angular2 withCredentials = true 在 safari 和 IE 上失败
angular2 withCredentials = true fails on safari and IE
我正在尝试使用网络服务进行身份验证并进行后续 api 调用。
export class HomeComponent {
newName: string;
headers = {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
};
constructor(public http: Http, public nameListService: NameListService) {
let _build = (<any>http)._backend._browserXHR.build;
(<any>http)._backend._browserXHR.build = () => {
let _xhr = _build();
_xhr.withCredentials = true;
return _xhr;
};
}
login(path, data) {
var creds = { 'Email': 'email@email.com', 'Password': 'pass', 'RememberMe': true };
this.http.post('http://xx.net/api/Users/Login', JSON.stringify(creds), this.headers)
.subscribe(res => {
console.log(res);
});
}
getdata() {
this.http.get('http://xx.net/api/Users/LoggedInUser', this.headers)
.subscribe(res => {
console.log(res);
}, (er) => {
console.log(er);
});
}
}
在构造函数中,我将 _xhr.credentails 设置为 true。
在 chrome 中一切正常,但在 IE 和 Safari 中失败。
当我尝试登录(在所有浏览器中)时,我可以看到 header 和 "Set-Cookie" 的响应,但是 safari 和 IE 似乎不会在后续请求中发送此 cookie,所以我得到 401。Chrome 正确执行此操作。
这是框架的错误吗?
可能是 Issue in this.withCredentials attribute in Cross Origin Resource Sharing
的翻版
允许第 3 方 cookie 的步骤:
IE 10 - Internet Options > Privacy > Advanced > Third Party Cookies > Accept
Safari - Preferences > Privacy > Block Cookies > Never
我正在尝试使用网络服务进行身份验证并进行后续 api 调用。
export class HomeComponent {
newName: string;
headers = {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
};
constructor(public http: Http, public nameListService: NameListService) {
let _build = (<any>http)._backend._browserXHR.build;
(<any>http)._backend._browserXHR.build = () => {
let _xhr = _build();
_xhr.withCredentials = true;
return _xhr;
};
}
login(path, data) {
var creds = { 'Email': 'email@email.com', 'Password': 'pass', 'RememberMe': true };
this.http.post('http://xx.net/api/Users/Login', JSON.stringify(creds), this.headers)
.subscribe(res => {
console.log(res);
});
}
getdata() {
this.http.get('http://xx.net/api/Users/LoggedInUser', this.headers)
.subscribe(res => {
console.log(res);
}, (er) => {
console.log(er);
});
}
}
在构造函数中,我将 _xhr.credentails 设置为 true。 在 chrome 中一切正常,但在 IE 和 Safari 中失败。 当我尝试登录(在所有浏览器中)时,我可以看到 header 和 "Set-Cookie" 的响应,但是 safari 和 IE 似乎不会在后续请求中发送此 cookie,所以我得到 401。Chrome 正确执行此操作。
这是框架的错误吗?
可能是 Issue in this.withCredentials attribute in Cross Origin Resource Sharing
的翻版允许第 3 方 cookie 的步骤:
IE 10 - Internet Options > Privacy > Advanced > Third Party Cookies > Accept
Safari - Preferences > Privacy > Block Cookies > Never