首页 > 解决方案 > 如何使用 ngx-cookie-service for Angular 在我的 cookie 中设置安全标志?

问题描述

当我创建它时,我想在我的 cookie 中设置安全标志。我想我有解决方案,但我想确定以继续。我使用ngx-cookie-service来设置我的 cookie。

这是我的代码:

const now = new Date();
now.setHours(now.getHours() + 8);
const secureFlag = true;
this.cookieService.set('usertype', 'agent', now, '/', '/', secureFlag);

问题是我不知道是否必须像这样声明第 4 和第 5 个参数,因为如果我不声明它们会显示错误。

例如我试试这个:

const now = new Date();
now.setHours(now.getHours() + 8);
const secureFlag = true;
this.cookieService.set('usertype', 'agent', now, secureFlag);

它警告我Argument of type 'true' is not assignable to parameter of type 'string'

当我不想定义它们时,是否必须使用'/'forpath和参数?domain

标签: angularcookiesngx-cookie-service

解决方案


SameSite这将起作用,如果您在使用 Google Chrome 时遇到问题,也可以在 Google Chrome 上测试默认禁用cookie。

将此粘贴到您的浏览器中,它将带您进入 SameSite 设置并禁用。

chrome://flags/#same-site-by-default-cookies

this.cookieService.set('sessionuser', username, 1 , '/', 'localhost', false, "Lax" );

推荐阅读