Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CorsAuthorizationFilterFactory in asp.net core 3

I am trying to migrate a project from asp.net Core 2.2.6 to asp.net core 3.0 In my startup I had

services.AddMvc(options =>
{
    options.Filters.Add(new CorsAuthorizationFilterFactory("default")); 
})

When I update to netcoreapp3.0 I receive the following error The type or namespace name 'CorsAuthorizationFilterFactory' could not be found (are you missing a using directive or an assembly reference?)

Does anyone know what is the equivalent to asp.net core 3?

like image 757
pantonis Avatar asked Nov 18 '25 11:11

pantonis


1 Answers

In ASP.NET Core 3.0 and later versions, CORS is configured differently than in earlier versions. The CorsAuthorizationFilterFactory is not used in the same way.

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("default", builder =>
        {
            builder.WithOrigins("yoursite")
                .AllowAnyHeader()
                .AllowAnyMethod();
        });
    });

And then in your configure method

app.UseCors("default"); 

This must rid of this error

like image 60
FarHard112 Avatar answered Nov 20 '25 01:11

FarHard112