Answers for "asp.net cors"

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
0

enable cors asp.net mvc

public static void Register(HttpConfiguration config)
{
    var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
    config.EnableCors(corsAttr);
}
Posted by: Guest on October-04-2020
0

how to enable cors policy in web api

BY LOVE
To enable CORS policy in web api, You need to add this method in your Global.asax file of API project. i.e
        
        protected void Application_BeginRequest()
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        }
Posted by: Guest on July-04-2020
0

enable cors asp.net mvc

public static void Register(HttpConfiguration config)
{
    // New code
    config.EnableCors();
}
Posted by: Guest on October-04-2020
0

enable cors asp.net mvc

Response.AppendHeader("Access-Control-Allow-Origin", "*");
Posted by: Guest on October-04-2020

C# Answers by Framework

Browse Popular Code Answers by Language