Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Biagio Paruolo 1567 posts 1796 karma points c-trib
    Oct 10, 2022 @ 11:20
    Biagio Paruolo
    0

    CORS in Umbraco 10: How to?

    I create this code in program.cs

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureUmbracoDefaults()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureServices(services => services.AddCors(options => options.AddPolicy("MyCors", policy =>
                    {
                        policy.AllowAnyOrigin()
                            .AllowAnyHeader()
                            .AllowAnyMethod();
                    })));
                    webBuilder.UseStaticWebAssets();
                    webBuilder.UseStartup<Startup>();
                });
    }
    

    and the enabled into startup.cs

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
    
            app.UseCors("MyCors");
            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
        }
    

    But it's not applied the CORS policy and there is not any doc about it. I use U10.2.1. How to? Thanks.

  • Biagio Paruolo 1567 posts 1796 karma points c-trib
    Oct 10, 2022 @ 12:36
    Biagio Paruolo
    101

    I create this and it works.

    Program.cs as by default code. Into the startup.cs:

    Into the ConfigureServices:

       services.AddCors(options =>
                {
                    //options.AddPolicy("AllowDashboardOrigin",
                    //    builder => builder.SetIsOriginAllowed((host) => true).WithOrigins("http://*:*", "https://*:*", "http://localhost:8080", "http://sebapi.redspace.eu", "https://sebapi.azurewebsites.net").AllowAnyMethod().AllowAnyHeader());
    
    
                    options.AddPolicy("AllowDashboardOrigin",
                                 builder => builder.SetIsOriginAllowed((host) => true).AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
    
                });
    

    Then in the Configure add before app.UseUmbraco()...:

    app.UseCors("AllowDashboardOrigin");
    
  • Dirk Seefeld 126 posts 665 karma points
    Dec 13, 2022 @ 10:22
    Dirk Seefeld
    0

    Works with version 11.0.0 too.

Please Sign in or register to post replies

Write your reply to:

Draft