DevNebulae
DevNebulae

Reputation: 4916

ASP.NET Core CORS configuration not working for Firefox

I have the following CORS configuration for my ASP.NET Core (dotnet SDK 3) application. What I observe is that this configuration works great for Google Chrome (version 76.0.3809.100 64-bit). The Access-Control-Allow-Origin is missing from the response headers in Mozilla Firefox Developer Edition (version 69.0b14 64-bit). As a matter of fact, there isn't any response. Is there any solution for this?

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers()
        .AddNewtonsoftJson();

    services.AddCors();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
    }

    app.UseCors(builder => 
        builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
    );

    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Upvotes: 2

Views: 1950

Answers (1)

DevNebulae
DevNebulae

Reputation: 4916

I found the problem and it was a really weird one. I was reading an older blog which talked about CORS and ASP.NET Core. Then I stumbled upon this comment which said that the solution worked for Kestrel, but not for IIS Express. So I changed my build configuration to not use IIS Express and it now works flawlessly for both browsers. If you want this to work for IIS Express, then you don't edit your Startup.cs, but you edit your application.config file which probably is located in your project root folder.

Upvotes: 1

Related Questions