ABP (AspNet Boilerplate) 的间歇性 CORS 策略问题 API

Intermittent CORS policy issue with ABP (AspNet Boilerplate) API

ASP.NET 零(.Net Core v2 + Angular v5)

AbpUserConfiguration/GetAll 有时会中断,在处理几个请求后它开始产生跨域问题,其他时候它运行良好。

错误如下

Access to XMLHttpRequest at 'http://localhost:22743/AbpUserConfiguration/GetAll' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

GET http://localhost:22743/AbpUserConfiguration/GetAll net::ERR_FAILED

appsettings.json

{
  "Origins": [
    "http://localhost:4200",
  ]
}

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("AllowedOrigins",
                    builder =>
                    {
                        builder
                            .WithOrigins(Configuration.GetSection("Origins").GetChildren().Select(c => c.Value)
                                .ToArray())
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials();
                    });
            });
        }
  1. ASP.NET 零已经正确配置了 CORS 起源允许列表,只需确保在 appsettings.json 文件中的 App:CorsOrigins 设置中输入正确的值即可。
{
  ...
  "App": {    
    "ServerRootAddress": "http://localhost:22743/",
    "ClientRootAddress": "http://localhost:4200/",
    "CorsOrigins": "http://localhost:4200",
    ...
  },
  ...
}
  1. 有时,错误消息具有误导性。它显示此错误是因为服务器端发生了一些错误。只需调查日志文件或调试并修复它,此消息就会消失。