【问题标题】:Set-cookie from server to client not working in node angular 8从服务器到客户端的设置cookie在节点角度8中不起作用
【发布时间】:2019-09-25 18:27:58
【问题描述】:

必须将标头中的 cookie 从节点发送到 Angular 客户端。默认它已经在 API 域中设置了 cookie。如果我在 cookie 中设置域,它就不起作用。

请说明设置cookie的正确方法是哪一种?

res.setHeader('Set-Cookie', ['token=' + token + ';httpOnly;Domain=abc.com;Path=/;secure:true;']);
res.cookie('token', token, { maxAge: 60*60*12, httpOnly: true,Domain=abc.com});     

【问题讨论】:

    标签: node.js angular api cookies cookie-httponly


    【解决方案1】:

    您的 API 和 Angular 应用程序是否位于不同的域中?如果是这样,您将必须通过 angular 的 http 调用传递 withCredentials 选项。 withCredentials 是一个布尔标志,指示是否应使用 cookie、授权标头或 TLS 客户端证书等凭据进行跨站点访问控制请求。此外,此标志还用于指示何时在响应中忽略 cookie。默认值为假。来自不同域的 XMLHttpRequest 无法为其自己的域设置 cookie 值,除非在发出请求之前将 withCredentials 设置为 true。更多详情MDN

    使用withCredentials 标志修改您的角度HTTP 调用。 对于 GET

    this.httpClient.get(url, { withCredentials: true})
    

    发帖

    this.httpClient.post(url, body, { withCredentials: true})
    

    【讨论】:

    • 是的,它在不同的域中,对不起,我忘了提,我已经在请求中添加了 withCredentials: true
    猜你喜欢
    • 2016-10-29
    • 2016-11-07
    • 1970-01-01
    • 2016-06-03
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多