调用ASP.NET Web API时,IIS中托管的Angular 11应用程序发行了CORS问题

发布于 2025-01-30 03:14:38 字数 814 浏览 1 评论 0原文

我们有以下设置

Angular 11/TypeScript App :与SSL/TLS CERT托管( https )在IIS中托管,并将用户凭据(ntlm)发送到后端API,并使用withCredentials头将其发送到后端API 。 同样对于CORS,我们正在传递标题“访问控制 - 允许 - 孔”:'*'

asp.net web api(.net 6) :使用http.sys(支持NTLM)进行自托管(以支持NTLM)并使用 http 。 为了支持CORS,

builder.Services.AddCors(options => options.AddDefaultPolicy(b => b.SetIsOriginAllowed(_ => true)
        .AllowAnyHeader()
        .AllowAnyMethod()
        .AllowCredentials()));

当Angular App呼叫后端API时,我们正在使用它,它会遇到与CORS相关的错误。

对飞行前请求的响应不通过访问控制检查:不允许重定向以送货前请求。

但是,当我们在没有SSL/TLS CERT的Angular应用程序IE HTTP的情况下运行Angular App时,它可以正常运行。

因此,此CORS问题是因为Angular App是HTTP,而后端API是HTTP?如果是,那么我们如何解决此问题?

提前致谢。

We have the following setup

Angular 11 / TypeScript app: Hosted in IIS with SSL/TLS cert (HTTPS) and sending user credentials (NTLM) to the backend API using withCredentials header.
Also for CORS we are passing header 'Access-Control-Allow-Origin' : '*'

Asp.Net Web API (.Net 6): Self-hosted using Http.Sys (to support NTLM) and using HTTP.
To support CORS, we are using this

builder.Services.AddCors(options => options.AddDefaultPolicy(b => b.SetIsOriginAllowed(_ => true)
        .AllowAnyHeader()
        .AllowAnyMethod()
        .AllowCredentials()));

When the Angular App is making a call to the backend API, it is getting error related to CORS.

Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

But when we run Angular app without SSL/TLS cert i.e HTTP, then it works fine.

So this CORS issue, is it happening because Angular app is HTTPS and backend API is HTTP? If yes then how can we solve this issue?

Thanks in advance.

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

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

发布评论

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

评论(1

瞎闹 2025-02-06 03:14:38

因此,这个问题问题是因为Angular App是HTTP,而后端API是HTTP?如果是,那么我们如何解决此问题?

TLS与非TLS请求的不匹配会引发这样的东西:

访问'https://another-site.com/'从origin'https://example.com'已被CORS策略阻止:不存在'access-control-allow-origin'标题在请求的资源上。

您会遇到此错误,因为前飞行选项请求是通过某种中间件路由重定向的(我假设)。

您可以在此处阅读有关如何配置选项前请求的选项。根据您的要求,有很多选择,因此我不会发布建议。

https://学习。 microsoft.com/en-us/aspnet/core/security/cors?view= aspnetcore-6.0#prefleflight-requests

So this CORS issue, is it happening because Angular app is HTTPS and backend API is HTTP? If yes then how can we solve this issue?

The mismatch of TLS to non-TLS requests throws something like this:

Access to fetch at 'https://another-site.com/' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

You are getting this error because the preflight OPTIONS request is being redirected by some sort of Middleware routing (I assume).

You can read up on how to configure for OPTIONS preflight requests here. There is lots of options depending on your requirements, so I won't post a suggestion.

https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0#preflight-requests

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