Answers for "asp.net core 5 allow cors all"

C#
5

asp.net core allow all origins

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy(MyAllowSpecificOrigins,
        builder =>
        {
            builder.WithOrigins("http://example.com",
                                "http://www.contoso.com")
                                .AllowAnyHeader()
                                .AllowAnyMethod();
        });
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Posted by: Guest on March-03-2020
2

asp.net allow cors for all

// Credit_1: https://www.infoworld.com/article/3327562/how-to-enable-cors-in-aspnet-core.html
// Credit_2: https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0

// At class "Startup":
#region global variables
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
#endregion

// Inside "ConfigureServices" function
// From a --SINGLE SOURCE-- (in this case localhost:4200):
services.AddCors(options =>
{
	options.AddPolicy(name: MyAllowSpecificOrigins,
	builder => builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader());
});

// From --ALL SOURCES--:
services.AddCors(options =>
{
	options.AddPolicy(name: MyAllowSpecificOrigins,
    builder => builder.AllowAnyOrigin());
});

// Inside "Configure" function
app.UseCors(MyAllowSpecificOrigins);
Posted by: Guest on August-03-2021

C# Answers by Framework

Browse Popular Code Answers by Language