Skip to content

Commit

Permalink
add cors composer
Browse files Browse the repository at this point in the history
  • Loading branch information
rolorogan committed Jul 24, 2024
1 parent b7f2a40 commit 7841b81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
18 changes: 18 additions & 0 deletions Wmca.Web/Composers/CorsComposer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Web.Common.ApplicationBuilder;

public class CorsComposer : IComposer
{
public const string AllowAnyOriginPolicyName = nameof(AllowAnyOriginPolicyName);

public void Compose(IUmbracoBuilder builder)
=> builder.Services
.AddCors(options => options.AddPolicy(AllowAnyOriginPolicyName, policy => policy.AllowAnyOrigin()))
.Configure<UmbracoPipelineOptions>(options => options.AddFilter(new UmbracoPipelineFilter("Cors", postRouting: app => app.UseCors())))
// For testing only
.Configure<UmbracoPipelineOptions>(options => options.AddFilter(new UmbracoPipelineFilter("CorsTest", endpoints: app => app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/echo", context => context.Response.WriteAsync("echo")).RequireCors(AllowAnyOriginPolicyName);
endpoints.MapGet("/echo2", context => context.Response.WriteAsync("echo2"));
}))));
}
21 changes: 2 additions & 19 deletions Wmca.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,12 @@
#endif

app.UseUmbraco()
// .WithMiddleware(u =>
// {
// u.UseBackOffice();
// u.UseWebsite();
// })
.WithCustomMiddleware(u =>
.WithMiddleware(u =>
{
u.RunPrePipeline();

u.UseUmbracoCoreMiddleware();
u.AppBuilder.UseUmbracoMediaFileProvider();
u.AppBuilder.UseStaticFiles();
u.AppBuilder.UseUmbracoPluginsStaticFiles();
u.AppBuilder.UseRouting();
// u.AppBuilder.UseCors(MyAllowSpecificOrigins);
u.AppBuilder.UseAuthentication();
u.AppBuilder.UseAuthorization();
u.AppBuilder.UseRequestLocalization();
u.AppBuilder.UseSession();

u.RunPreRouting();
u.RunPostRouting();
u.RunPostPipeline();
u.RunPostRouting();
u.UseBackOffice();
u.UseWebsite();
})
Expand Down

0 comments on commit 7841b81

Please sign in to comment.