为什么我的代理将与我的API不同的端口重定向?

发布于 2025-02-06 18:38:27 字数 900 浏览 2 评论 0原文

我浪费了一个多星期的同事,而我在这个问题上的时间。任何帮助将不胜感激。谢谢您提前的时间。 这是使用Visual Studio 2022创建的设置:

Web应用程序(预生产代码,我几乎没有触摸配置和程序。 >,身份。当我启动时,命令也表示SPA代理也正在启动蜜蜂,而IIS给我的URL为https:// localhost: 44384 。所以我去那里。在那儿,它说:“水疗代理将您将您重定向到https:// localhost: 44484 ”。 为什么?它确实将我重定向到44484。起初,我不在乎,我开始使用控制器,UI等。它根本无法阻止API完全工作。但是现在,我在泡菜。

因为,我现在需要从控制器那里获得用户电子邮件。非常基本的东西对吗? httpContextAccessor,您已经完成了。但是!因为端口是不同的!因此,要使它起作用,您不仅需要允许所有CORS,因此在我的情况下是不可能的,因为我们的服务器管理员不会让这种情况发生,而且您还需要在Angular Interceptor中删除检查(>授权。interceptor.ts)检查请求是否具有与window.location.host相同的来源。对我来说,这感觉就像删除安全检查。

因此,我觉得我的解决方案在于那个港口的故事。我的同事从头开始重新创建了一个项目以进行比较,他的应用程序与API同一端口上启动了代理!这是怎么回事?我不想从头开始。我想了解是否可以控制代理启动的端口。我尝试简单地编辑编号 44484 到处都是 44384 ,但命令将永远循环。

我不明白,我找不到在线答案,就像我是唯一没有得到这个的开发人员一样。 请任何建议。谢谢大家。

I've wasted more than two weeks of one of my colleague's and my time on this issue. Any help would be greatly appreciated. Thank you for your time in advance.
Here is the setup:

Web app created with Visual Studio 2022 (pre-generated code, I hardly touched the configuration and the Program.cs), .Net Core 6.0, Angular 13, Identity. When I launch, a command indicates that the SPA Proxy is beeing launched as well, and the URL given to me by IIS is https://localhost:44384. So I go on there. On there, it says that "the SPA Proxy will redirect you to https://localhost:44484". Why? It indeed redirects me to 44484. At first, I didn't care, I started working on my controllers, my UI, etc. It was not preventing the API from working at all. But now, I'm in a pickle.

Because, I now need to get the user email from my controller. Pretty basic stuff right? HttpContextAccessor and you're done. But NO! Because the ports are different! So, to make it work, you not only need to allow all CORS, which is not possible in my case because our servers admin won't let that happen, but you also need to remove a check in the Angular interceptor (authorize.interceptor.ts) that checks that the request bears the same origin as the window.location.host. This, to me, feels like removing a security check.

So I feel my solution lies with that port story. My colleague recreated a project from scratch, to compare, and his app launches the proxy on the same port as the API! What's going on? I don't want to start from scratch. And I want to understand if I can control the port the proxy is launched on. I tried simply editing the number 44484 everywhere it was in the code to 44384 but the command would just loop forever.

I don't understand and I can't find an answer online, like I'm the only developer not getting this.
Please any advice. Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-02-13 18:38:27

所以,我也有相同的反应,为什么我需要重定向?
这就是所谓的代理权,我希望一切都会在同一端口上运行,而代理将修复所有这些。

事实证明,这是副设计,这是相反的。 Angular代理您的API请求。如果您查看de ClientApp/Angular文件夹中的proxy.conf.js文件,您将获得这个想法。

const { env } = require('process');

const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
 env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:38142';

const PROXY_CONFIG = [
 {
   context: [
     "/weatherforecast",
  ],
   target: target,
   secure: false,
   headers: {
     Connection: 'Keep-Alive'
   }
 }
]

module.exports = PROXY_CONFIG;

有关此信息的更多信息: https://angular.io/guide/构建#代理到靠背服务器

So, I had the same reaction, why do I need to be redirected?
It's called a proxy right, I expected that everything would run on the same port and the proxy would fix all this.

Turns out that this is by-design, and it is the other way around. Angular proxies the request to your API. If you take a look at the proxy.conf.js file in de ClientApp/Angular folder you will get the idea.

const { env } = require('process');

const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
 env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:38142';

const PROXY_CONFIG = [
 {
   context: [
     "/weatherforecast",
  ],
   target: target,
   secure: false,
   headers: {
     Connection: 'Keep-Alive'
   }
 }
]

module.exports = PROXY_CONFIG;

More info on that here: https://angular.io/guide/build#proxying-to-a-backend-server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文