Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 966 karma points c-trib
    Jun 13, 2023 @ 23:31
    Murray Roke
    0

    Hijacking multiple doctypes with one controller

    I have completely custom authentication, so I'm not using any umbraco auth/members.

    I see I can add a custom controller for a single doctype like so:

    https://docs.umbraco.com/umbraco-cms/reference/routing/custom-controllers

    So, in order to require auth for a given page I add the [Authorize] attribute to a custom controller like so:

    [Authorize]
    public class MyBookingsPageController : RenderController
    {
        public MyBookingsPageController(
            ILogger<MyBookingsPageController> logger,
            ICompositeViewEngine compositeViewEngine,
            IUmbracoContextAccessor umbracoContextAccessor)
            : base(logger, compositeViewEngine, umbracoContextAccessor)
        {
        }
    
        public override IActionResult Index()
        {
            // you are in control here!
            // return a 'model' to the selected template/view for this page.
            return CurrentTemplate(CurrentPage);
        }
    }
    

    I have about 5 doctypes which would all require an identical controller just to add one attribute.

    Is there a better way?

    For example, can I assign multiple doctypes to one controller?

  • Simon Napper 115 posts 349 karma points
    Jun 20, 2023 @ 23:34
    Simon Napper
    0

    How about a shared base class that inherits RenderController? You could set the attribute in there?

  • Murray Roke 503 posts 966 karma points c-trib
    Jul 03, 2023 @ 23:15
    Murray Roke
    0

    This works, but I was looking for something more terse.

    I realised I don't need the Index() method in either case also, so I can drop that. This would be a valuable solution if my base class was doing more work.

    I suppose I shouldn't worry too much it's only a few lines anyway :-D

  • Roy Berris 89 posts 577 karma points c-trib
    Jun 21, 2023 @ 12:15
    Roy Berris
    1

    Hi,

    You could create a DefaultRenderController : RenderController. Which is the same as your controller above.

    Then add this to your configure services.

    services.Configure<UmbracoRenderingDefaultsOptions>(c =>
    {
        c.DefaultControllerType = typeof(DefaultRenderController);
    });
    

    This will route everything to the default render controller unless a controller is specified for given doctype. If you keep your MyBookingsPageController then that doctype will still be routed to that controller but all others will go to the DefaultRenderController.

    Now you can populate the DefaultRenderController with the [Authorize] property. Note that everything will now need to authorize. If this isn't the case I suggest a base controller like Simon described.

Please Sign in or register to post replies

Write your reply to:

Draft