Send request for static .htm file through custom auth filter (or the equivalent of that)

Viewed 39

I have an ASP.NET MVC app, in which I use a custom authorization filter throughout several parts of the app.

[MyCustomAuthorize]
class MyController
{
}

This has worked well for a long while.

Now I want to add another piece to the app which is basically a pre-built React app - a subfolder structure containing an index.htm file, JS, CSS, and other resource files.

When a user navigates to this sub-app, I want them to be sent through the same authorization process implemented in my custom authorization attribute. But naturally, since it's a static .htm file, I have no way of applying any attribute to it.

I tried to resolve this by placing it behind an MVC controller:

using MyApp.Filters;
using System.Web.Mvc;

namespace MyApp.Controllers
{
    [MyCustomAuthorize]
    public class SubAppController : Controller
    {
        public ActionResult Index()
        {
            return File("~/SubApp/index.htm", "text/html");
        }
    }
}

but it seems that what happens if I navigate to https://mydomain/SubApp/ is that IIS finds the index.htm file and serves that up instead of the request going through the controller.

If I rename the index.htm to something else and try to serve it this way:

using MyApp.Filters;
using System.Web.Mvc;

namespace MyApp.Controllers
{
    [MyCustomAuthorize]
    public class SubAppController : Controller
    {
        public ActionResult Index()
        {
            return File("~/SubApp/subapp.htm", "text/html");
        }
    }
}

then it seems IIS sees the https://mydomain/SubApp/ request as an attempt to access the physical SubApp/ folder, and returns a 403 response.

I realize I could get around these two issues by physically placing the sub-app contents in a folder with a different name from the controller, but that would introduce yet another problem where all of the sub-app's resource files (CSS, JS, etc.) are no longer in a sub-path of the path where the sub-app is being accessed (i.e. the browser would be accessing https://mydomain/SubApp/ and the .css would be in https://mydomain/SubAppFiles/styles.css). I would like to avoid that situation if possible, and have all of the resources (both physically and conceptually) contained within the SubApp/ folder.

Is there a relatively simple way to get this to work nicely without too much overhaul to the main app?

I think the ideal situation would be to find some way to have IIS not try to handle the request to https://mydomain/SubApp/ (just that one path and nothing else) and allow the ASP.NET app to handle it, but as of yet, I've been unsuccessful in finding a way to do that.

0 Answers
Related