Answers for "asp.net web.config allow cors"

C#
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