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 1593 posts 1824 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 1593 posts 1824 karma points c-trib
    Oct 10, 2022 @ 12:36
    Biagio Paruolo
    103

    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");
    
  • Johannes Lantz 156 posts 838 karma points c-trib
    Nov 30, 2023 @ 12:56
    Johannes Lantz
    1

    I had to place app.UseCors("AllowDashboardOrigin"); inside WithMiddleware in order to make it work

            app.UseUmbraco()
                .WithMiddleware(u =>
                {
                    app.UseCors("AllowDashboardOrigin");
                    u.UseBackOffice();
                    u.UseWebsite();
                })
                .WithEndpoints(u =>
                {
                    u.UseInstallerEndpoints();
                    u.UseBackOfficeEndpoints();
                    u.UseWebsiteEndpoints();
                });
    
  • Dirk Seefeld 126 posts 665 karma points
    Dec 13, 2022 @ 10:22
    Dirk Seefeld
    0

    Works with version 11.0.0 too.

  • Chris Spanellis 45 posts 189 karma points
    Oct 27, 2023 @ 17:52
    Chris Spanellis
    0

    Can anyone expand on whether or not the order of when this happens matters? Does this need to happen before all of the Umbraco Builder calls when adding?

    And what if we're using UseHsts and/or UseHttpsRedirection. Does the order matter of when we call UseCors?

Please Sign in or register to post replies

Write your reply to:

Draft