[英]Cookie does not generate when calling my asp.net core controller from my react app and returning null when try to get it
This is my code for frontend which calls for my session这是我的前端代码,需要我的 session
await fetch("https://localhost:5001/api/session", {
method: "GET",
credentials: "same-origin",
})
This is my code for the api when sets a session这是我设置 session 时 api 的代码
[HttpGet]
public ActionResult<string> CallMe()
{
HttpContext.Session.SetString("Who", "Doctor Who");
return Ok();
}
This should set a cookie in my browser on call but nothing shows up there.Code when I try to get the cookie on my other controller这应该在我的浏览器中设置一个 cookie,但没有显示在那里。当我尝试在我的另一个 controller 上获取 cookie 时的代码
[HttpGet]
public ActionResult<string> CallMeToo()
{
string a = HttpContext.Session.GetString("Who");
return Ok();
}
This returns null after the set call I call this too.这在设置调用后返回 null 我也调用它。
Solved by adding AllowCredentials on services.AddCors to allow cross-origin cookies通过在 services.AddCors 上添加 AllowCredentials 来允许跨域解决 cookies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.