【问题标题】:angular2 withCredentials = true fails on safari and IEangular2 withCredentials = true 在 safari 和 IE 上失败
【发布时间】:2017-05-12 15:53:47
【问题描述】:

我正在尝试使用 web 服务进行身份验证并进行后续的 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 中失败。 当我尝试登录(在所有浏览器中)时,我可以看到带有“Set-Cookie”的响应标头,但是 safari 和 IE 似乎没有随后续请求发送此 cookie,所以我得到 401。Chrome 正确地做到了。

这是框架的错误吗?

【问题讨论】:

  • 如果浏览器兼容性是问题,很可能是bug。我认为这个问题更适合用 plunker 解决 GitHub 问题。

标签: xmlhttprequest angular


【解决方案1】:

可能是Issue in this.withCredentials attribute in Cross Origin Resource Sharing的重复

允许 3rd 方 cookie 的步骤:

IE 10 - Internet 选项 > 隐私 > 高级 > 第三方 Cookies > 接受

Safari - 首选项 > 隐私 > 阻止 Cookie > 从不

【讨论】:

  • 在angular1中,我没有这个问题,有没有办法解决这个问题
  • 那么这可能是您遇到的另一个问题。我不认为 Angular 版本在这里有什么可做的。
猜你喜欢
  • 1970-01-01
  • 2013-03-06
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-29
  • 2012-02-05
  • 1970-01-01
相关资源
最近更新 更多