简体   繁体   English

Javascript 获取不发送 JWT httponly cookie 到.Net Core 6 API

[英]Javascript Fetch not sending JWT httponly cookie to .Net Core 6 API

I've been working on implementing JWT and refresh_tokens.我一直致力于实现 JWT 和 refresh_tokens。 The latest attempt uses refresh_tokens stored in an httpOnly cookie.最新的尝试使用存储在 httpOnly cookie 中的 refresh_tokens。 My API is a.Net Core 6 API, and using Swagger and Postman, things seem to work fine.我的 API 是 .Net Core 6 API,使用 Swagger 和 Postman,似乎一切正常。 I see the refresh_token cookie in the browser devtools Application Tab.我在浏览器 devtools 应用程序选项卡中看到了 refresh_token cookie。 In Swagger and Postman, when I run my refresh_token endpoint, which uses the httpContextAccessor to get the Request.Cookies, it's fine.在 Swagger 和 Postman 中,当我运行我的 refresh_token 端点时,它使用 httpContextAccessor 获取 Request.Cookies,没问题。 I see in the Request Headers, a cookie with my refresh_token.我在请求标头中看到一个带有我的 refresh_token 的 cookie。 Now, my attempts to replicate this work in VSCode running a live server is where I'm running into trouble.现在,我尝试在运行实时服务器的 VSCode 中复制这项工作时遇到了麻烦。 I'm using a vanilla JS front end and Fetch.我正在使用 vanilla JS 前端和 Fetch。 My login code works fine:我的登录代码工作正常:

fetch(loginUrl, {
   method: "POST",
   credentials: "include",
   body: JSON.stringify(authBody),
   headers: {
      "Content-Type": "application/json; charset=utf-8"
});

The Browser dev tools shows my cookie is fine.浏览器开发工具显示我的 cookie 没问题。 My attempts to call my refresh_token endpoint however does NOT send the cookie.我尝试调用我的 refresh_token 端点但是不发送 cookie。 I don't see a cookie in the Request Headers.我在请求标头中没有看到 cookie。 I've been trying some variations of the above fetch code but the cookie won't go. Now the only thing I can think of is a cors issue... My API is running from VS2022 and is at https://localhost:7121 and my VSCode Live Server is http://localhost:5500.我一直在尝试上述获取代码的一些变体,但 cookie 不会 go。现在我唯一能想到的是 cors 问题...我的 API 从 VS2022 运行,位于 https://localhost: 7121,我的 VSCode 实时服务器是 http://localhost:5500。

On the API side, I've tried setting the cookie with:在 API 方面,我尝试使用以下方法设置 cookie:

HttpOnly = true,
Secure = true,
Expires = expireTime,

I've tried including some variations of SameSite too, but not much luck.我也尝试过包含 SameSite 的一些变体,但运气不佳。 Any suggestions or help is greatly appreciated!非常感谢任何建议或帮助!

OK, it looks like I got the cookie to work... it seems the "SameSite" property for the cookie was messing it up.好的,看起来我的 cookie 可以正常工作了……似乎 cookie 的“SameSite”属性搞砸了。 I just noticed a checkbox in the Network tab in the Devtools that showed filtered out request cookies, and it indicated SameSite being strict was preventing the cookie from going.我刚刚注意到 Devtools 的“网络”选项卡中的一个复选框显示已过滤掉请求 cookies,它表明 SameSite 严格阻止了 cookie。 I set it to none in my API, and it looks like the cookie is sent now... the only issue now is the response I get back is opaque, so I don't get my new tokens from the API.我在我的 API 中将它设置为无,看起来 cookie 现在已发送...现在唯一的问题是我返回的响应是不透明的,所以我没有从 API 获得我的新令牌。

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

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