Angular & Web API: 'Access-Control-Allow-Origin’ header contain multiple values.(blocked by CORS policy).

Aman Sharma
Access to XMLHttpRequest at 'http://core/api/test/filter?id=0' from origin
'' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values 'http://testcom, ,', but only one is allowed.

1.       Temporary solution for testing or development purpose: Disable web security by following command.

Open Run  --> Type Following Command

  ‘chrome.exe --user-data-dir="C://Chrome dev session" --disable-web-security’

New chrome will open, run your website or application in this browser. It will work fine. Browser will look like this:

2.       When Issue occurred on Server: Add custom header & following settings in web config file in   <system.webServer> section:

        <add name="Access-Control-Allow-Origin" value="," />
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, HEAD, POST, PUT, DELETE, OPTIONS" />

3.       In Startup.cs: add following settings in

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            using (var serviceScope = app.ApplicationServices.CreateScope())
                var context = serviceScope.ServiceProvider.GetRequiredService<DBContext>();
            app.UseCors(options => options.WithOrigins("").AllowAnyMethod().AllowAnyHeader().AllowCredentials().AllowAnyOrigin());
            if (env.IsDevelopment())

Add url, where you are consuming these webApi. So it will allow CORS.

       Dont use solution 2 & 3 together, in this way settings will be implemented twice & it will give same error.

       Hope this article will help you to sort out this issue. In case of any query or further issue, you can comment
      in comment section below.

Post a Comment

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !