Lädt...

🔧 Yet another CORS in Umbraco Post


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

I don't know how many blog, forum, and Discord posts I've read about how to set up CORS for a headless site in Umbraco. It feels like a lot. None of them worked for me 😥

It's probably just the usual problem of incorrect middleware order, which is always slightly tricky in Umbraco as some of the pipeline configuration is abstracted away.

What am I trying to do?

POST a form, via the Fetch API, from the frontend of my Headless site to my Umbraco instance backend.

The Problem

CORS. It's there to keep us all safe, but it can be a pain to configure.

An error message from a CORS failure

Fortunately there's a built-in CORS middleware in ASP.NET Core that can be configured

The Catch

I can't modify Startup.cs. Well, I could but it would ruin my project's architecture. The headless features are added in a separate assembly to the site's Core and Web code. The idea is that we can just drop the headless library into a baseline project to "switch on" headless functionality.

The Solution

Below is what is now working for me.

This composer lives in my BaseProject.Headless library and adds the appropriate CORS, so long as a frontend URI is set.

public class Composer : IComposer
{
    private const string FrontendCorsPolicyName = "FrontendCorsPolicy";

    public void Compose(IUmbracoBuilder builder)
    {

        if (Uri.TryCreate(builder.Config.GetSection("Headless:FrontendUrl")?.Value, UriKind.Absolute, out Uri? frontendUri))
        {
            builder.Services.AddCors(options =>
            {
                options.AddPolicy(FrontendCorsPolicyName, corsPolicyBuilder =>
                {
                    corsPolicyBuilder.WithOrigins(frontendUri.ToString())
                    .SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost")
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            builder.Services.Configure<UmbracoPipelineOptions>(options =>
            {
                options.AddFilter(new UmbracoPipelineFilter(FrontendCorsPolicyName)
                {
                    PrePipeline = app =>
                    {
                        app.UseCors(FrontendCorsPolicyName);
                    },
                });
            });
        }
    }
}

Of special note is this:

corsPolicyBuilder.SetIsOriginAllowed(origin => 
  new Uri(origin).Host == "localhost"
)

This is here because we can't guarantee devs running the frontend locally will be using any specific port (or scheme) - this should probably be wrapped in an environment check.

...

🔧 Yet another CORS in Umbraco Post


📈 57.87 Punkte
🔧 Programmierung

🔧 Reusing Umbraco Properties in Umbraco v14


📈 35.23 Punkte
🔧 Programmierung

🔧 Not All CORS Errors Are CORS Errors


📈 30.28 Punkte
🔧 Programmierung

🔧 CORS Tester - Test CORS Online


📈 30.28 Punkte
🔧 Programmierung

🔧 Why mode: "no-cors" won't fix your CORS errors


📈 30.28 Punkte
🔧 Programmierung

🔧 CORS Anywhere Alternative: Free vs. Premium CORS Proxy


📈 30.28 Punkte
🔧 Programmierung

🔧 Cross-Origin Resource Sharing(CORS). CORS middleware Setup.


📈 30.28 Punkte
🔧 Programmierung

🔧 Introduction to CORS (Cross-Origin Resource Sharing) What is CORS?


📈 30.28 Punkte
🔧 Programmierung

🔧 Understanding CORS, CSRF attacks and enabling valid CORS


📈 30.28 Punkte
🔧 Programmierung

📰 CORS: How to Use and Secure a CORS Policy with Origin


📈 30.28 Punkte
📰 IT Security Nachrichten

🕵️ rack-cors up to 0.4.0 CORS Request privilege escalation


📈 30.28 Punkte
🕵️ Sicherheitslücken

🕵️ rack-cors bis 0.4.0 CORS Request erweiterte Rechte


📈 30.28 Punkte
🕵️ Sicherheitslücken

📰 Another day, another update, another iPhone lockscreen bypass


📈 25.81 Punkte
📰 IT Security Nachrichten

🐧 Yet another &quot;Why I went back from Sway (Wayland) to i3 (X11)&quot; post


📈 25.11 Punkte
🐧 Linux Tipps

🐧 Yet Another Post Inquiring on the Banality of Windows Installation compared to Linux.


📈 25.11 Punkte
🐧 Linux Tipps

🐧 Yet another old school UNIX book post!


📈 25.11 Punkte
🐧 Linux Tipps

🔧 Datavere Web Api: how to setup CORS? NO ANSWER YET...


📈 24.98 Punkte
🔧 Programmierung

📰 E-POST Portal, E-POST Cloud und E-POST App werden eingestellt


📈 20 Punkte
📰 IT Nachrichten

📰 Post-Truth, Post-West, Post-Order: Der Hack der Weltordnung


📈 20 Punkte
📰 IT Nachrichten

🔧 So... I created yet another Neovim plugin


📈 18.45 Punkte
🔧 Programmierung

🔧 Yet Another Newsletter LOL: Broccoli


📈 18.45 Punkte
🔧 Programmierung

🔧 Yet Another Newsletter LOL: H2 Oh!


📈 18.45 Punkte
🔧 Programmierung