locked
CORS policy error - angular and web api - localhost - WEB API CORE 3.0 RRS feed

  • Question

  • User-1350042179 posted

    Hi

    I tried to debug my angular application(localhost) with my web api app(localhost) - net core 3, but I received cors error:

    This is the error.

    Would could I debug?

    Saturday, February 8, 2020 4:22 AM

Answers

  • User-1350042179 posted

    Hi

    I fixed it with the following conffiguration in Startup class.

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
               app.UseCors("CorsPolicy"); 
            }
      public void ConfigureServices(IServiceCollection services)
            {
               
    
                //Cors
                services.AddCors(options =>
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder
                        .SetIsOriginAllowed((host) => true)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());
                });
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 8:39 PM

All replies

  • User475983607 posted

    Would could I debug?

    Do do basic troubleshooting.

    View the server response in the browser's dev tools (network) to see if you are receiving the developer error page from the Web API application.  Read the error and fix.

    Are you using Windows authentication?  If so enable Anonymous Authentication.

    Verify you've configured CORS correctly on the server by read the openly published documentation. https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1

    Saturday, February 8, 2020 10:49 AM
  • User-474980206 posted

    Your website at localhost:44340 needs to implement CORS and needs to allow origin localhost:4200

    The other option is to have localhost:4200 proxy the requests to :44340

    Saturday, February 8, 2020 5:57 PM
  • User-1350042179 posted

    Hi

    I fixed it with the following conffiguration in Startup class.

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
               app.UseCors("CorsPolicy"); 
            }
      public void ConfigureServices(IServiceCollection services)
            {
               
    
                //Cors
                services.AddCors(options =>
                {
                    options.AddPolicy("CorsPolicy",
                        builder => builder
                        .SetIsOriginAllowed((host) => true)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());
                });
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 8:39 PM