cookieService.deleteAll()删除除此cookie外的所有cookie

时间:2019-07-19 08:36:58

标签: angular cookies

Image enter image description here

我有两个角度应用程序,一个在localhost:4200上运行,另一个在本地主机xampp上运行,它是laravel角度应用程序。 单击第一个应用程序中的链接将打开指向第二个应用程序的链接,在第二个应用程序中,用户将基于令牌进行身份验证。第一个应用程序中有一个注销按钮,该按钮应从第二个应用程序以及第一个应用程序中注销用户,为此,我需要清除所有cookie。 当用户单击第一个应用程序的链接时,第二个应用程序将设置laravel_session cookie。

最后一个cookie laravel_session将保留,而其他所有cookie都将被删除。

free(x->first);
free(x->last);
// DO NOT free(x->count), it's an integer, not a previously allocated pointer!
free(x);

2 个答案:

答案 0 :(得分:0)

您正在尝试从客户端代码中删除仅Http cookie。 根本不可能。

在生成cookie时使用HttpOnly标志有助于减轻客户端脚本访问受保护cookie的风险。 因此,如果HTTP响应标头中包含HttpOnly标志(可选),则无法通过客户端脚本访问cookie

答案 1 :(得分:0)

在开发环境localhost中;任何使用 Angular 或其他 UI 框架的客户端应用程序都需要调整服务器会话 cookie 代码,如下所示。

注意:在您的后端服务器上,您需要将 httpOnly 的 cookie 设置改为 False,如下代码

cookie: {
       secure: false, //set this to true in production over https
       httpOnly: false, //set this to false in development to test delete 
       ....
       ....
}

稍后,在用于注销方法的 Angular 服务/组件代码中,如下所示:

logout(){

this.cookieService.delete('<your-cookie-name>', '/', 'localhost', false, 'Lax');

}

See ngx-cookie-service documentation

对于个人删除:

delete( name: string, path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax'): void;

对于批量删除:

deleteAll( path?: string, domain?: string, secure?: boolean, sameSite: 'Lax' | 'None' | 'Strict' = 'Lax' ): void;

重要:在生产中两者都应设置为 True | httpOnly & 安全 |对于 Cookie