简体   繁体   中英

CORS issue after upgrading to DotNet 3.1 while connecting from localhost to server API

I get the following error after migrating the project to DotNet Core 3.1 in the browser when I attempt to connect to my API (DotNet Core 3.1) hosted on IIS from my local system - localhost:4200

Access to XMLHttpRequest at 'https://dev.ncop.firstam.net/multisiteservice/api/v1/test/method' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is the CORS code snippet from my startup.cs

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

Here is my 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
  }

How do I connect from my local system to the server API? What am I missing. The UI application attempting to connect to the API is build on Angular 5

According to the docs

the URL must not contain a trailing slash (/). If the URL terminates with /, the comparison returns false and no header is returned.

Your domains in AppSettings should not contain a trailing forward-slash ( / ).

Can you try it with this one:

 "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
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM