我正在尝试从浏览器读取 cookie 以设置 authguard。我正在尝试检查是否设置了 cookie,然后用户是否已登录,否则不会。为此,我使用 ngx-cookie-service。这是从 cookie 中获取值(value)的代码。
import {UserAuthService} from '../services';
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class AuthService {
constructor(
private cookieService: CookieService,
}
getSessionid() {
this.cookieService.set('test', '123');
console.log(this.cookieService.get('test'));
console.log(this.cookieService.get('sessionid'));
console.log(this.cookieService.get('csrftoken'));
console.log(this.cookieService.get('messages'));
return this.cookieService.get('sessionid') || null;
}
}
在上面的代码中,它在控制台中正确打印了某些值,例如 test 和 csrftoken,但是消息和 sessionid 是空白的,而在应用程序选项卡中,我可以看到所有的 cookie。这是我的应用程序选项卡的屏幕截图。
这是我的控制台的屏幕截图
有人知道这里出了什么问题吗?
最佳答案
如果你从未找到答案——
那是因为未打印的 cookie 是“httpOnly”。您可以在第一个屏幕截图的第 7 列中勾选这些 cookie。
https://owasp.org/www-community/HttpOnly
关于angular - 无法读取某些 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51044963/