简体   繁体   English

从我的 React 应用程序调用我的 asp.net 核心 controller 并在尝试获取它时返回 null 时不会生成 Cookie

[英]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.

相关问题 为什么我的 model 中的这个值在我的 controller 中使用时返回 null? (ASP.NET MVC) - Why does this value in my model return null when used in my controller? (ASP.NET MVC) 当我尝试使用 ASP.NET 核心标识创建新用户时,为什么我的 ApiKey 变量位于 null 中? - Why can it be that my ApiKey variable is in null when I try to create a new user using ASP.NET Core Identity? 检索ASP.NET Core 2.1 Cookie项时为null - ASP.NET Core 2.1 Cookie Item null when retrieved 当我从具有多个 forms 的视图提交表单时,为什么在我的 controller 中收到 null 值? 带 ASP.NET 内核 5 MVC - Why do I receive null values in my controller when I submit a form from a view with multiple forms? With ASP.NET Core 5 MVC 为什么我从 ASP.net Core 2.2 调用的 SOAP 调用返回 null 结果? - Why is my SOAP call from ASP.net Core 2.2 returning a null result? 在 ASP.NET Core 中发布到控制器时,所有值都为空 - All values are null when posting to controller in ASP.NET Core 当我尝试以天蓝色发布我的ASP.NET CORE Web应用程序时出现问题-无法加载文件或程序集&#39;Microsoft.Extensions.Identity.Core - Problem when i try to publish my ASP.NET CORE web app in azure - Could not load file or assembly 'Microsoft.Extensions.Identity.Core 我的 ASP.NET Core 应用没有出现在 Centos 服务器上 - My ASP.NET Core app does not appear on Centos server 我的ASP.NET核心应用程序成功启动或不启动时记录 - Log when my ASP.NET Core app launches successfully or when it doesn't Angular和ASP.NET MVC-当我将参数传递给后端控制器时参数为null - Angular & ASP.NET MVC - Parameter is null when I pass a parameter to my backend Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM