简体   繁体   English

Azure 应用服务 API 中的“请求的资源上不存在‘访问控制允许来源’header”

[英]"No 'Access-Control-Allow-Origin' header is present on the requested resource" from Azure App Service API

Note: Please don't mark as duplicate as you can see from my code I have done exactly what the other answers say.注意:请不要像从我的代码中看到的那样标记为重复,我已经完全按照其他答案所说的那样做了。

I have a web API for backend and a Node.js app for frontend.我有一个用于后端的 web API 和一个用于前端的 Node.js 应用程序。

Both of them are hosted on Microsoft Azure App Services.它们都托管在 Microsoft Azure 应用服务上。

Both of them have run fine locally, but recently I have deployed them to Azure and I am getting the following error when trying to call the API from the Node.js app:它们都在本地运行良好,但最近我已将它们部署到 Azure 并且尝试从 Node.js 应用程序调用 API 时出现以下错误:

Access to XMLHttpRequest at '[API endpoint URL]' from origin '[https://....azurewebsites.net]'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the
requested resource.

I have hidden the actual URL of the site but I can confirm it is identical to the one shown in the code below:我隐藏了该站点的实际 URL 但我可以确认它与下面代码中显示的相同:

Here are the related parts of Startup.cs in the Web API:下面是Web API中Startup.cs的相关部分:

...

readonly string[] origins = { "http://localhost:3000", "https:/....azurewebsites.net" };

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddCors(options =>
    {
        options.AddDefaultPolicy(policy =>
        {
            policy.WithOrigins(origins)
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials();
        });
    });

    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpsRedirection();
    app.UseSerilogRequestLogging();
    app.UseRouting();
    app.UseCors();
    app.UseAuthentication();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Here is the endpoint that I am trying to access这是我要访问的端点

[Route("api/[controller]")]
[ApiController]
public class AuthController : BaseController
{
    ...
    [AllowAnonymous]
    [HttpGet]
    [Route("[action]")]
    [ProducesResponseType(typeof(string[]), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> Organizations(string username)
    {
        ...
    }
}

I also think its important to mention that I can acces the enpoint locally even when not running the Node.js on port 3000, even though that's the only one that's listed in origins.我还认为值得一提的是,即使不在端口 3000 上运行 Node.js,我也可以在本地访问 enpoint,即使这是 origins 中列出的唯一一个。

The issue was that even though CORS was enabled in the application code, there is also a CORS page in the Azure portal where a list of origins must be added.问题是,即使在应用程序代码中启用了 CORS,Azure 门户中也有一个 CORS 页面,必须添加来源列表。

To find it just Search for 'CORS' in the Azure portal search bar and add the URL of the site that you would like to have access to your endpoints.要找到它,只需在 Azure 门户搜索栏中搜索“CORS”,然后添加您希望访问端点的站点的 URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CORS所请求的资源上没有“ Access-Control-Allow-Origin”标头 - CORS No 'Access-Control-Allow-Origin' header is present on the requested resource CORS - 没有'Access-Control-Allow-Origin' header 存在于请求的资源上 - CORS - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource 问题:请求的资源上不存在“Access-Control-Allow-Origin”header - Issue: No 'Access-Control-Allow-Origin' header is present on the requested resource 调用我的 API 时在我的反应应用程序上出现此 CORS 错误 - 请求的资源上不存在“Access-Control-Allow-Origin” header - Getting this CORS error on my react app when calling my API - No 'Access-Control-Allow-Origin' header is present on the requested resource Web API中的请求资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource in web api c#已启用CORS的Web Api和所请求资源上存在可怕的“Access-Control-Allow-Origin”标头 - c# Web Api with CORS Enabled and the dreaded No 'Access-Control-Allow-Origin' header is present on the requested resource 使用 http 访问 API 时,请求的资源错误中不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource error while accessing the API using http MVC web api:请求的资源上不存在“Access-Control-Allow-Origin”标头 - MVC web api: No 'Access-Control-Allow-Origin' header is present on the requested resource 当服务器返回 401 响应时,请求的资源上不存在“Access-Control-Allow-Origin”header - No 'Access-Control-Allow-Origin' header is present on the requested resource when 401 response is returned from the server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM