.NET CORE 2.0 Angular 5: Allowing Cors

change your line in API with :

app.UseCors(builder => builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowAnyCredentials());

be sure you added Services.AddCors(); in ConfigureServices() stop the server and run again after changes are made.


"WithOrigins" expect an array, not a string so maybe this is your case. However the minimum requirements for Cors to works in your case are:

In Startup.cs to add services.AddCors(); before services.AddMvc(); and also:

string[] origins = new string[] { "http://localhost:4200" }; app.UseCors(b=>b.AllowAnyMethod().AllowAnyHeader().WithOrigins(origins));

Again add it before app.UseMvc(your routes ...)

Or what you actually need doesn't matter the technology is to add a "Access-Control-Allow-Origin" header with value the origin/origins in the response of the server which in .Net core 2 can be done like this (in any method in a controller):
ControllerContext.HttpContext .Response .Headers .Add("Access-Control-Allow-Origin","http://localhost:4200");

or globally - you can create a middleware that add this header to all the responses when the origin match. Works also in Angular 6 and .Net Core 2 as separate applications.


Changed builder.WithOrigins("http://localhost:4200/") to

builder.WithOrigins("http://localhost:4200")

(Removed the '/')