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 162 posts 586 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 158 posts 766 karma points c-trib
    Mar 17, 2022 @ 14:37
    Ambert van Unen
    101

    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 239 posts 428 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 158 posts 766 karma points c-trib
    Mar 17, 2022 @ 19:18
    Ambert van Unen
    1

    Yes it is :-)

Please Sign in or register to post replies

Write your reply to:

Draft