【问题标题】:Intermittent CORS policy issue with ABP (AspNet Boilerplate) APIABP (AspNet Boilerplate) API 的间歇性 CORS 策略问题
【发布时间】:2020-07-28 17:40:58
【问题描述】:

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

AbpUserConfiguration/GetAll 有时会中断,在服务了几个请求后,它开始产生跨域问题,其他时候它工作得很好。

以下是错误。

从源“http://localhost:4200”访问“http://localhost:22743/AbpUserConfiguration/GetAll”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。

GET http://localhost:22743/AbpUserConfiguration/GetAllnet::ERR_FAILED

【问题讨论】:

    标签: angular asp.net-core aspnetboilerplate


    【解决方案1】:
    1. ASP.NET Zero 已经正确配置了 CORS Origins 允许列表,只需确保您在 appsettings.json 文件中的 App:CorsOrigins 设置上设置了正确的值。
    {
      ...
      "App": {    
        "ServerRootAddress": "http://localhost:22743/",
        "ClientRootAddress": "http://localhost:4200/",
        "CorsOrigins": "http://localhost:4200",
        ...
      },
      ...
    }
    
    1. 有时,错误消息具有误导性。它显示此错误是因为服务器端发生了一些错误。只需调查日志文件或调试并修复它,此消息就会消失。

    【讨论】:

    • 谢谢!我会检查并回复你。
    • #2 对我有用.. 这是与 Redis cahce 超时有关的一些问题,我在调试模式下禁用了它的初始化部分.. 现在似乎工作正常。谢谢!
    • @ANKIT 如果这解决了你的问题,你能考虑accepting it吗?
    • @ANKIT 你能告诉我你如何在调试模式下禁用它,因为我有同样的问题吗?
    【解决方案2】:

    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();
                        });
                });
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 2021-11-22
      • 2020-04-06
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多