CORS with ASP.NET Core RC2

cors with ASP.NET Core RC2

Overview

I have just moved my microservice to RC2. However some time ago I have already built JQuery call from webite to this microservice and I had to configure CORS in order to do this. This doesn’t work anymore with RC2. So how to configure CORS with ASP.NET Core RC2?

BTW. Take a look on Server for HTML and Gulp build pipeline if you want to run this.

Current state

Running my app currently will result in following error on website:

XMLHttpRequest cannot load http://localhost:5000/api/user. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

CORS with ASP.NET Core RC1

using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNet.Cors;

namespace Shout.UserService
{
    public class Startup
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                        .AllowAnyMethod()
                                                                         .AllowAnyHeader()));                                                                                                                                                  
            
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseCors("AllowAll");
            app.UseMvc();            
        }
    }
}

CORS with ASP.NET Core RC2

It’s almost exactly the same 🙂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Microsoft.AspNetCore.Cors;

namespace Shout.UserService
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
              var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath);
                
            Configuration = builder.Build();
        }
        
        public IConfigurationRoot Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                        .AllowAnyMethod()
                                                                         .AllowAnyHeader()));     
            services.AddMvc();            
        }
    
    public void Configure(IApplicationBuilder app)
        {
            app.UseCors("AllowAll");
            
            app.UseMvc(routes =>
             {
                 routes.MapRoute(
                     name: "default",
                     template: "{controller=Home}/{action=Index}/{id?}");
             });
            
        }
    }
}

4 thoughts on “CORS with ASP.NET Core RC2

  • June 14, 2016 at 10:06 am
    Permalink

    What’s the difference?

    Reply
    • September 7, 2016 at 6:13 pm
      Permalink

      The namespace name has been changed from AspNet to AspNetCore 😉

      Reply
  • October 6, 2016 at 9:16 pm
    Permalink

    Michal, were you ever able to resolve this? I am running into the exact same issue with a Service Fabric-hosted microservice using ASP.NET Core MVC.

    Reply
  • October 9, 2016 at 9:05 pm
    Permalink

    Yes… as above in previous comment. In this case this was only about new namespace: Microsoft.AspNetCore.Cors; instead of using Microsoft.AspNet.Cors;

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *