使用Passport Azure AD时会出现CORS错误

发布于 2025-01-29 06:34:49 字数 574 浏览 1 评论 0原文

我拥有我的Express API服务器以及Heroku上的水疗中心。该水疗中心使用MSAL使用Azure AD身份验证。使用Passport-azure-AD确保API。一切都在我的本地主机上运作良好,并在获得成功响应的地方提出了要求。但是,在将更改上传到Heroku时,我现在会遇到CORS错误。我已经在Azure AD上注册了API,并将API暴露于我的水疗中心。我仍然会遇到错误的原因吗?

Access to fetch at 'https://myTestApp-server.herokuapp.com/getUsers' from origin 'https://myTestApp.herokuapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I have my Express API server as well as the SPA hosted on Heroku. The SPA uses Azure ad authentication using MSAL. The APIs are secured using Passport-azure-ad. Everything was working well in my localhost and the requests made where getting a success response. However on uploading the changes to Heroku I am now getting the CORS error. I have registered the API on Azure AD and exposed the API to my SPA. Any reason why I am still getting the error?

Access to fetch at 'https://myTestApp-server.herokuapp.com/getUsers' from origin 'https://myTestApp.herokuapp.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

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

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

发布评论

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

评论(1

黑色毁心梦 2025-02-05 06:34:49

要避免CORS错误,请确保添加标题如下:

  • ,同时添加允许的起源使用 * 以允许所有类似下面的内容:

< a href =“ https://i.sstatic.net/hv95f.png” rel =“ nofollow noreferrer”>

  • 否则,添加特定的起源如下:

“在此处输入图像说明”

  • 在进行上述更改后,请确保重新启动 API。
  • 检查您是否使用了与您正在开发和测试的运行时相匹配的nodejs的正确版本
  • 尝试通过添加 .setisoriginallowed(((host)=&gt; true)来修改 cors 策略
  • 。下面的命令:
res.setHeader('Access-Control-Allow-Origin', '*');

res.setHeader('Access-Control-Allow-Origin',
https://myTestApp-server.herokuapp.com:4200');

有关更多详细信息,请请参阅下面的链接:

azure app服务no access-control-allow-allow-origin'标头

no'访问访问权限控制 - 供电-Allow-Origin'标头是在Angular App中的请求资源上存在

3种固定CORS错误的方法以及访问control-allow-allow-allow-hower header如何工作

To avoid the CORS error, make sure to add the header like below:

  • While adding the Allowed origins use * to allow all like below:

enter image description here

  • Otherwise, add particular origins like below:

enter image description here

  • Make sure to restart the Api after making the above changes.
  • Check whether you are using correct version of NodeJs that matches the runtime you’re developing and testing.
  • Try modifying CORS policy by adding .SetIsOriginAllowed((host)=> true)"
  • If you are adding header via Nodejs code make use of below command:
res.setHeader('Access-Control-Allow-Origin', '*');

Or

res.setHeader('Access-Control-Allow-Origin',
https://myTestApp-server.herokuapp.com:4200');

For more in detail, please refer below link:

Azure App Service No ‘Access-Control-Allow-Origin’ header is present

No 'Access-Control-Allow-Origin' header is present on the requested resource in angular app

3 Ways to Fix the CORS Error and How the Access-Control-Allow-Origin Header Works

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