-1

I'm currently working on the .Net core project. My front-end as angular. I have handled the CORS on the server side, in fact, it was working earlier after JWT implementation facing this error.

Error: Access to XMLHttpRequest at 'https://localhost:5001/api/users' from origin 'https://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCors(x=>x.AllowAnyHeader().AllowAnyMethod().WithOrigins("https://localhost:4200/"));

        app.UseAuthentication();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    
    }
     public void ConfigureServices(IServiceCollection services)
    {

        services.AddApplicationServices(_config);
        services.AddControllers();


        // services.AddCors(optpolicy => optpolicy.AddPolicy().AllowAnyMethod().WithOrigins("https://localhost:4002/"));
        services.AddCors();
        
        services.AddIdentityServices(_config);


        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
        });
    }

    getUsers()
 {
   this.http.get('https://localhost:5001/api/users').subscribe(response=>{
  this.users=response;
  },error=>{console.log(error);
   }
   );
   }
Serge
  • 40,935
  • 4
  • 18
  • 45
Thiyagarajan
  • 327
  • 1
  • 6
  • 21
  • Does this answer your question? [Deadly CORS when http://localhost is the origin](https://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin) – Liam Mar 10 '21 at 17:04
  • Chrome doesn't support localhost as a CORS address – Liam Mar 10 '21 at 17:04
  • @Liam Thanks for your suggestion, but same error encountered in Microsoft edge also – Thiyagarajan Mar 10 '21 at 17:32
  • Same error after changing localhost to 127.0.0.1 - Access to XMLHttpRequest at 'https://localhost:5001/api/users' from origin 'https://127.0.0.1:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. – Thiyagarajan Mar 10 '21 at 17:35

1 Answers1

0

You are using the wrong syntax and the wrong port number 4002. Try this

ConfigureServices:

services.AddCors(o => o.AddPolicy("AllowSomeOrigins", builder =>
            {
                builder.WithOrigins("https://localhost:4200")
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

notice that you shouldn't use a trailing "/" in url and this code should be at the top of the section.

Configure:

app.UseCors("AllowSomeOrigins");

leave in the same place.

Serge
  • 40,935
  • 4
  • 18
  • 45