3.0: Enable CORS

In order to allow our client to send requests and receive responses, we need to enable Cross-Origin Request Sharing (CORS).

In the Startup class in our .API project, add the following middleware in the Configure() method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());      //  <---- Added
    app.UseAuthentication();
    app.UseMvc();
}

This makes our API wide open, which makes testing easier. For deployment, you may consider changing this.

Last updated