@manhng

Welcome to my blog!

Enable CORS in ASP.NET Core WebAPI

November 20, 2017 15:45

Enabling CORS in ASP.net Core

http://www.c-sharpcorner.com/article/enabling-cross-origin-requests-in-asp-net-core/

http://www.c-sharpcorner.com/article/enable-cross-origin-resource-sharing-cors-in-asp-net-core/

https://tahirnaushad.com/2017/09/09/cors-in-asp-net-core-2-0/

https://dotnetcoretutorials.com/2017/01/03/enabling-cors-asp-net-core/

Enabling CORS in ASP.net Core

https://stackoverflow.com/questions/44379560/how-to-enable-cors-in-asp-net-core-webapi

Because you have a very simple CORS policy (Allow all requests from XXX domain), you don't need to make it so complicated. Try doing the following first (A very basic implementation of CORS).

If you haven't already, install the CORS nuget package.

Install-Package Microsoft.AspNetCore.Cors

In the ConfigureServices method of your startup.cs, add the CORS services.

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
}

Then in your Configure method of your startup.cs, add the following :

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseCors(
        options => options.WithOrigins("http://example.com").AllowAnyMethod()
    );

    app.UseMvc();
}

Categories

Recent posts