英文:
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
在与前端开发人员的紧密合作后,我们发现解决此问题的最佳方法是:
- 从模板中删除默认代理,也就是 删除 setupProxy.js(我们还删除了 aspnetcore-https.js 和 aspnetcore-react.js,但我认为与代理无关,它们只是对我们的情况多余的)。
- 在 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:
- 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).
- In packages.json we added a new property:
"proxy": "https://localhost:44497/"
And this apparently solved our cookie problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论