繁体   English   中英

Angular 10:set-cookie 响应标头被忽略?

[英]Angular 10: set-cookie response headers being ignored?

我在登录后尝试使用 session 身份验证 cookie,但未保存该 cookie。

为了进行测试,我在我的 Do.net Core 服务器上创建了一个简单的“CookieTest”方法:

[Route("CookieTest")]
public ActionResult CookieTest()
{
  var options = new CookieOptions()
  {
    SameSite = SameSiteMode.None,
    Secure = true
  };
  Response.Cookies.Append("NoPermsCookie", "cookie set");
  Response.Cookies.Append("SameSiteSecure", "cookie set", options);
  return new OkResult();
}

如果我从普通浏览器 window(使用 Chrome)发出 GET 请求,我会看到set-cookie标头,并且 cookies 显示在“应用程序”开发工具页面中。

如果我从我的 Angular 应用程序运行一个简单的测试:

this.authService.getCookieTest().subscribe(() => console.log('Got response, check cookies!'));

public getCookieTest() {
  return this.http.get(`${this.urlBase}/api/auth/cookietest`);
}

我收到请求成功的日志消息; 我在响应中看到了set-cookie标头; 但实际上并未设置 cookie 值。

以下是来自开发工具的请求标头: 在此处输入图像描述

我确定我做错了什么(CORS 配置?请求中缺少 header?),但我不知道是什么。 我确定我在 Angular 的早期版本中使用过此功能,但很长时间没有尝试过。

有什么建议么?

它永远不会失败,在你提出问题后不久你就会得到答案......

查看一篇关于使用HttpInterceptor 强制执行 withCredentials的文章,我在测试中意识到我将其设置为 header,而不是作为 HttpClient get/post 请求的选项。

正确使用 withCredentials,一切正常。

[捂脸]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM