0
votes

I have a project with multiple .net core projects. I am having an issue when making a call from project A to project B. I keep getting a cors issue stating no-access-control-allow-origin is present. i have this setup below which is not working for some reason.

 public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddCors();
            services.AddMvc().AddMvcOptions(options => options.RespectBrowserAcceptHeader = true);

        }

   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAntiforgery antiforgery)
        {
            loggerFactory.AddDebug(LogLevel.Information).AddSerilog();



            app.UseCors(builder =>
              builder.WithOrigins("*")
              .WithMethods("*")
              .AllowAnyHeader()
              .AllowAnyMethod()
              .AllowAnyOrigin()
              );

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

            });
        }
2

2 Answers

1
votes

The wildcard doesn't work.

try this:

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

https://docs.microsoft.com/en-us/aspnet/core/security/cors#set-the-allowed-origins

0
votes

This is how I do it. Works well. What JimmyH suggested should work, I just wanted to give you another option.

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("AnyOrigin", builder =>
        {
            builder
                .AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod();
        });
    });

    services.AddMvc();
    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseCors("AnyOrigin");
    app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

            });
    ...
}