如何在React前端和.NET后端位于不同端口时传递cookie?

huangapple go评论63阅读模式
英文:

How to pass cookie when the frontend in React and backend in .NET are on different ports?

问题

在一段时间前,我决定制作一个.NET 6 Web应用程序,使用"ASP.NET Core with React.js"。我有一个公司强制执行的SSO,通过身份验证将所有声明保存在cookie中。问题是,如果我将.NET应用程序放在44497端口,React应用程序放在44496端口,那么React应用程序无法访问cookie。当我部署到Azure时,一切都好像在一个端口上运行,这意味着这个问题不会出现。

但在本地开发应用程序时,我该如何处理呢?

英文:

Some time ago I decided to make a .NET 6 web application using the "ASP.NET Core with React.js". I have a company-enforced SSO which on authenticating is saving all the claims in the cookie. The problem is if I have the .NET application on port 44497, and the React app on 44496 then the React app is unable to access the cookie. When I deploy into Azure everything acts as if it works on one port, which means that this problem will not be appearing.

But how do I handle that when developing the application on localhost?

答案1

得分: 0

在与前端开发人员的紧密合作后,我们发现解决此问题的最佳方法是:

  1. 从模板中删除默认代理,也就是 删除 setupProxy.js(我们还删除了 aspnetcore-https.js 和 aspnetcore-react.js,但我认为与代理无关,它们只是对我们的情况多余的)。
  2. 在 packages.json 中,我们添加了一个新属性:

"proxy": "https://localhost:44497/"

这显然解决了我们的Cookie问题。

英文:

After some close cooperation with a fellow front end developer we discovered that the best problem to solve this issue will be to:

  1. Delete the default proxy from the template, meaning delete setupProxy.js (we also deleted aspnetcore-https.js and aspnetcore-react.js, but I think it was not related to the proxy, they were just redundant for our case).
  2. In packages.json we added a new property:

"proxy": "https://localhost:44497/"

And this apparently solved our cookie problem.

huangapple
  • 本文由 发表于 2023年5月13日 23:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定