【问题标题】:Chrome does not send cookieChrome 不发送 cookie
【发布时间】:2018-05-23 07:02:25
【问题描述】:

我正在尝试使用角度 5 将数据发布到服务器。 我的代码是:

createProfile(newProfile: NewProfile) {
    this.http.post<NewProfile>(this.messageUrlPost, CreateFormData.createFormData(newProfile), {withCredentials: true, observe: 'response'}).subscribe(resp => {
        // Read the result field from the JSON response.
        console.log(resp.body); 
        });
}

为了能够执行请求,我需要通过身份验证 cookie。所以我使用: withCredentials: true。但是由于某种原因,这在 Chrome 上不起作用(它拒绝发送 cookie)。在 Firefox 和 Edge 上它确实有效。

【问题讨论】:

  • 您使用的是哪个版本的 Angular??
  • @Aravind ^5.0.3
  • 你应该导入模块HttpClientXsrfModule并设置配置
  • 我这样配置它:` HttpClientXsrfModule.withOptions({ cookieName: '.AspNetCore.Identity.Application', headerName: 'Cookie' })` 但是它仍然不起作用。也许我没有正确配置它。没有很多关于如何使用 HttpClientXsrfModule 的文档
  • 您是否可以在 teamviewer 或远程连接中进行调试并帮助您解决此问题

标签: angular google-chrome authentication cookies


【解决方案1】:

我发现了问题。

npm startlocalhost 上打开站点,我在192.168.0.15 上调用了api(在这种情况下,它与localhost 是同一台PC,但我需要为本地网络打开api)

Firefox 和 Edge 可以使用此设置。然而 Chrome 不是。所以我补充说:

"defaults": {
    "serve": {
        "host": "192.168.0.15"
},

.angular-cli.json 以使网站加载到192.168.0.15(与api 地址相同)而不是默认的localhost,结果Chrome 开始发送cookie。

【讨论】:

    猜你喜欢
    • 2019-01-14
    • 2017-04-08
    • 2016-02-02
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多