Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 260 posts 1001 karma points c-trib
    Mar 17, 2022 @ 14:00
    Martin Rud
    0

    How to add header with "Access-Control-Allow-Origin: *" in Umbraco 9?

    Hi,

    I have some JavaScript that needs to have access to external json. How do I configure my site to allow this?

  • Ambert van Unen 175 posts 819 karma points c-trib
    Mar 17, 2022 @ 14:37
    Ambert van Unen
    102

    I've added a little bit of middleware so I can add custom headers.

    for example: startup.cs

           app.UseMiddleware<CustomHeaders>();
           app.UseUmbraco() ......
    

    CustomHeaders.cs

     public class CustomHeaders{
     private readonly RequestDelegate _next;
    
        public CustomHeaders(RequestDelegate next)
        {
            _next = next;
        }
    
        public async Task InvokeAsync(HttpContext context)
        {
            context.Response.Headers.Add("X-Frame-Options", "sameorigin");
            context.Response.Headers.Add("X-Xss-Protection", "1; mode=block");
            context.Response.Headers.Add("X-Content-Type-Options", "nosniff");
    
            await _next(context);
        }}
    

    You could add your Access-Control-Allow-Origin in a similar way.

  • Carlos Mosqueda 244 posts 435 karma points
    Mar 17, 2022 @ 17:25
    Carlos Mosqueda
    0

    That is a very nice dolution Ambert.

    This actually answered a bit of my questions. Is this compiled into the "projectName.dll" ?

  • Ambert van Unen 175 posts 819 karma points c-trib
    Mar 17, 2022 @ 19:18
    Ambert van Unen
    1

    Yes it is :-)

  • Chris Lord 58 posts 211 karma points
    Dec 01, 2022 @ 09:53
    Chris Lord
    0

    I have followed this but can't see the headers and health check still reports these, any ideas what I might be missing ?

Please Sign in or register to post replies

Write your reply to:

Draft