Copied to clipboard

Flag this post as spam?

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


  • David Walker 19 posts 90 karma points
    May 16, 2022 @ 13:01
    David Walker
    0

    Umbraco V9 - CORS - How should it be handled

    I have some APIs created and they need to be called from known servers. How do I handle CORS in V9 - I couldn't find anything in the documentation (hopefully me just missing it somehow).

    Many thanks,

    Dave

  • Simon Justesen 74 posts 193 karma points
    May 17, 2022 @ 13:07
    Simon Justesen
    0

    Take a look at this thread :)

  • David Walker 19 posts 90 karma points
    May 18, 2022 @ 12:49
    David Walker
    0

    Thanks Simon. I had in fact seen that post in my Googling and forum search before posting here.

    I think that this kind of thing should be more formally part of the V9 documentation. I'll have a check through, and may jump onto the Git repo with that as a suggestion / see if i can add to it.

    Thanks again.

  • Jules 269 posts 560 karma points
    May 19, 2022 @ 17:05
    Jules
    0

    Hi David

    I have same requirement as you.

    Did you manage to get this working? I haven't had any joy so far with implementing.

    Jules

  • Jules 269 posts 560 karma points
    May 20, 2022 @ 10:43
    Jules
    1

    Thanks to Andy Butland and his blog article Customising the Umbraco Pipeline.

    This is how to do it for Cors:

    Create a PipelineFilter:

    public CorsPipelineFilter() : base(nameof(CorsPipelineFilter))
    {
        PostPipeline = app =>
        {
            app.UseCors(policy => policy
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin());
    
            //Also works
            //app.UseCors(policy => policy.WithOrigins("http://localhost:36261"));
        };
    }
    

    Add filter to Pipeline:

    public class ConfigureCorsOptions : IConfigureOptions<UmbracoPipelineOptions>
    {
        public void Configure(UmbracoPipelineOptions options)
        {
            options.AddFilter(new CorsPipelineFilter());
        }
    }
    

    in Startup in ConfigureServices method:

    services.ConfigureOptions<ConfigureCorsOptions>();
    services.AddCors();
    
  • Dirk Seefeld 126 posts 665 karma points
    Dec 13, 2022 @ 10:06
    Dirk Seefeld
    0

    Thanks Jules,

    this made it clear! Btw. I have just tested it with Umbraco version 11.0.0

    But actually this post (https://our.umbraco.com/forum/using-umbraco-and-getting-started/110188-cors-in-umbraco-10-how-to) is an even better solution, because it follows the standard way of doing it and requires less effort.

    Dirk

Please Sign in or register to post replies

Write your reply to:

Draft