gpt4 book ai didi

azure - 禁用Azure应用服务的CORS中间件

转载 作者:行者123 更新时间:2023-12-04 04:42:19 24 4
gpt4 key购买 nike

我编写了一些代码来将 CORS 处理到我的 Web Api(.Net Core) 中。这些代码在我的本地和测试服务器(Azure 应用程序服务)上完美运行,但在生产(Azure 应用程序服务)上不起作用。它给出了错误-

XMLHttpRequest cannot load http://myapiproduction.co/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myuidashboard.co' is therefore not allowed access.

我在 appsettings.Production.json 中给出了正确的允许源 URL。我从 Azure 应用服务(生产环境)的 CORS 部分删除了所有值 (*)。

enter image description here

当我们从应用服务的 CORS 部分删除所有内容时,我们的代码逻辑应该可以工作,因为一次只有一个中间件可以工作应用服务的 CORS 中间件或我们的 Web Api 代码逻辑。

Web Api (Startup.cs)-

var allowedOriginsArr = Configuration["AppSettings:AllowedOrigins"].Split(','); //Example- "http://myuidashboard.co"
services.AddCors(options =>
{
options.AddPolicy("AllowAllCorsPolicy",
builder => builder.WithOrigins(allowedOriginsArr)
.WithMethods("GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS")
.AllowAnyHeader()
.WithExposedHeaders("X-Auth-Token")
.AllowCredentials());
});

所以,我的问题是 - 是否需要进行任何其他设置才能禁用 Azure 应用服务的 CORS 中间件?我想完全通过代码(Api 逻辑)控制 CORS

最佳答案

我认为没有任何额外的设置可以从 Azure 应用服务端显式禁用它。 Microsoft 本身建议使用 CORS 实用程序而不是内置实用程序 - Refer Here .

文档提供的注释 -

Don't try to use App Service CORS and your own CORS code together. When used together, App Service CORS takes precedence and your own CORS code has no effect.

关于azure - 禁用Azure应用服务的CORS中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com