从本地主机连接到服务器API时升级到DotNet 3.1后出现CORS问题

时间:2020-10-20 13:22:25

标签: .net-core cors .net-core-3.1

当我尝试从本地系统-localhost:4200连接到IIS上托管的API(DotNet Core 3.1)时,将项目迁移到浏览器中的DotNet Core 3.1后,出现以下错误

CORS策略已阻止从来源“ http:// localhost:4200”访问“ https://dev.ncop.firstam.net/multisiteservice/api/v1/test/method”上的XMLHttpRequest。预检请求未通过访问控制检查:所请求的资源上没有“ Access-Control-Allow-Origin”标头。

这是我的startup.cs中的CORS代码段

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                var allowedOrigin = Configuration["AppSettings:CorsAllowedOrigin"];
                {
                    options.AddPolicy("SiteCorsPolicy",
                    builder => builder.AllowAnyOrigin()
                    .WithOrigins(allowedOrigin)
                    .AllowAnyMethod()
                    .AllowCredentials()
                    .AllowAnyHeader());
                }
            });
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
            {
app.UseCors("SiteCorsPolicy");
            app.UseAuthentication();
            app.UseMvc();
            //Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();
    }

这是我的AppsettingsFile:

 "AppSettings": {
    "CorsAllowedOrigin": "http://localhost:4200/,https://dev.ncop.fistam.com/,https://staging.ncop.fistam.com/,https://ncop.fistam.com,http://azuvnintfint551.fastts.firstam.net,https://staging.webservices.firstam.net
  }

如何从本地系统连接到服务器API?我想念什么。 尝试连接到API的UI应用程序是基于Angular 5构建的。

1 个答案:

答案 0 :(得分:0)

根据docs

URL不得包含斜杠(/)。如果网址终止 使用/时,比较返回false,并且不返回标头。

您在 Container( alignment: Alignment.topCenter, height: 70, child: Card( color: Colors.white, child: Padding( padding: const EdgeInsets.all(10.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ InkWell( child: Column( children: <Widget>[ Text("123"), Text("123"), ], ), ), Container( height: 40, child: VerticalDivider( color: Colors.grey, width: 15, ), ), InkWell( onTap: () {}, child: Container( width: 300, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text("123"), Text("123"), ], ), ), ), ], ), ), ), ), 中的域中不应包含尾随的正斜杠(AppSettings)。

您可以用这个尝试一下吗?

/