如何使用 Angular 的 ngx-cookie-service 在我的 cookie 中设置安全标志?

The*_*Tsg 5 cookies angular ngx-cookie-service

我想在创建 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);
Run Code Online (Sandbox Code Playgroud)

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

例如我试试这个:

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

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

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

小智 6

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

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

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

this.cookieService.set('sessionuser', username, 1 , '/', 'localhost', false, "Lax" );
Run Code Online (Sandbox Code Playgroud)


小智 1

这篇文章是否解决了同样的问题?我想那里的答案可以帮助你。 Angular4x:带有过期参数的 ngx-cookie-service