Why is Azure Front Door breaking content-type headers that are set in a HttpHandler?

198 Views Asked by At

We are running an ASP.NET MVC 5 app on .NET 4.8 in Azure. I setup a HttpHandler to add a header to static files. It works without issue on our app service that does not have Front Door, but on the one that does, it seems the Content-Type header is getting lost on some of the files inconsistently.

The only thing consistent is if the HttpHandler is hit, the problem arises (I can set my variable ConfigHelper.IsUsingAccessControlAllowOrigin to false to skip the addition of the header and it still occurs).

Static files are not getting loaded due to this error:

Refused to execute script from 'https://example.com/Scripts/select2.full.js' because its MIME type ('') is not executable, and strict MIME type checking is enabled.

Here is the HttpHandler code:

public class CorsHttpHandler : IHttpHandler
{
    protected RequestContext RequestContext { get; set; }

    public CorsHttpHandler() : base() { }

    public CorsHttpHandler(RequestContext requestContext)
    {
        this.RequestContext = requestContext;
    }

    public void ProcessRequest(HttpContext context)
    {
        if (ConfigHelper.IsUsingAccessControlAllowOrigin && !context.Response.Headers.AllKeys.Any(x => x.Equals(ConfigHelper.AccessControlAllowOriginKey, StringComparison.OrdinalIgnoreCase)))
        {
            context.Response.AddHeader(ConfigHelper.AccessControlAllowOriginKey, ConfigHelper.WildCardAsterik);
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

I configured the HttpHandler like this in the web.config (side-bar: I set them this way because the comma delimited path would not work):

<add name="CorsHttpHandlerCss" 
     verb="*" path="*.css" 
     type="Example.Mvc.Handlers.CorsHttpHandler, Example.Mvc" 
     preCondition="managedHandler" />
<add name="CorsHttpHandlerJs" 
     verb="*" path="*.js" 
     type="Example.Mvc.Handlers.CorsHttpHandler, Example.Mvc" 
     preCondition="managedHandler" />
<add name="CorsHttpHandlerJpg" 
     verb="*" path="*.jpg" 
     type="Example.Mvc.Handlers.CorsHttpHandler, Example.Mvc" 
     preCondition="managedHandler" />
<add name="CorsHttpHandlerPng" 
     verb="*" path="*.png" 
     type="Example.Mvc.Handlers.CorsHttpHandler, Example.Mvc" 
     preCondition="managedHandler" />

One option to resolve this is to abandon the setting of this header in C#, buy the Premium instance of Front Door / CDN and then set the header there. However, I think this needs to be solved because how are people using HttpHandlers with a Front Door without this issue?

0

There are 0 best solutions below