Cookie 未发送 front-end 至 back-end
Cookies don't get sent front-end to back-end
我在 http://localhost:3000
上安装了 React front-end 运行ning,在 https://localhost:44378
上安装了 ASP.NET Core back-end。尽管我在 Chrome:
中看到它们,但我没有在请求中看到它们
这是一些请求headers:
sec-fetch-mode: cors
sec-fetch-site: cross-site
我尝试自己设置 cookie:
HttpContext.Response.Cookies.Append("my1", "cookie.None", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.None, Secure = true,
});
HttpContext.Response.Cookies.Append("my2", "cookie.Lax", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.Lax, Secure = true,
});
HttpContext.Response.Cookies.Append("my3", "cookie.Strict", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.Strict, Secure = true,
});
我在 Chrome 中看到它们:
set-cookie: my1=cookie.None; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=none
set-cookie: my2=cookie.Lax; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=lax
set-cookie: my3=cookie.Strict; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=strict
但是他们没有出现在table上面,也没有出现在back-end.
这是我尝试阅读它们的方式:
app.Use(async (context, next) =>
{
foreach (var p in context.Request.Cookies)
{
log.Info($"{p.Key} - {p.Value}");
}
log.Info(context.Request.Path);
await next.Invoke();
});
编辑:将 front-end 更改为我得到的 https
sec-fetch-mode: cors
sec-fetch-site: same-site
尽管 port should not be an issue 仍然无效。但是当我在同一个过程中 运行 front-end 和 back-end 时,不需要cors它工作正常。
我的请求似乎被认为是 CORS,所以我做了两件事:
- 在服务器端,我在
services.AddCors(...)
调用中添加了 .AllowCredentials()
。这会将 access-control-allow-credentials: true
添加到响应中。
- 在客户端,我指示
axios
使用 axios.get(..., { withCredentials: true })
在请求中包含凭据。
- 使用
HTTPS=true npm start
启动前端,让它们都在 https 上。
我在 http://localhost:3000
上安装了 React front-end 运行ning,在 https://localhost:44378
上安装了 ASP.NET Core back-end。尽管我在 Chrome:
这是一些请求headers:
sec-fetch-mode: cors
sec-fetch-site: cross-site
我尝试自己设置 cookie:
HttpContext.Response.Cookies.Append("my1", "cookie.None", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.None, Secure = true,
});
HttpContext.Response.Cookies.Append("my2", "cookie.Lax", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.Lax, Secure = true,
});
HttpContext.Response.Cookies.Append("my3", "cookie.Strict", new CookieOptions() {
Expires = DateTime.UtcNow.AddYears(1), SameSite = SameSiteMode.Strict, Secure = true,
});
我在 Chrome 中看到它们:
set-cookie: my1=cookie.None; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=none
set-cookie: my2=cookie.Lax; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=lax
set-cookie: my3=cookie.Strict; expires=Sun, 29 May 2022 15:28:59 GMT; path=/; secure; samesite=strict
但是他们没有出现在table上面,也没有出现在back-end.
这是我尝试阅读它们的方式:
app.Use(async (context, next) =>
{
foreach (var p in context.Request.Cookies)
{
log.Info($"{p.Key} - {p.Value}");
}
log.Info(context.Request.Path);
await next.Invoke();
});
编辑:将 front-end 更改为我得到的 https
sec-fetch-mode: cors
sec-fetch-site: same-site
尽管 port should not be an issue 仍然无效。但是当我在同一个过程中 运行 front-end 和 back-end 时,不需要cors它工作正常。
我的请求似乎被认为是 CORS,所以我做了两件事:
- 在服务器端,我在
services.AddCors(...)
调用中添加了.AllowCredentials()
。这会将access-control-allow-credentials: true
添加到响应中。 - 在客户端,我指示
axios
使用axios.get(..., { withCredentials: true })
在请求中包含凭据。 - 使用
HTTPS=true npm start
启动前端,让它们都在 https 上。