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?
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
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.
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:
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?
How about a shared base class that inherits RenderController? You could set the attribute in there?
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
Hi,
You could create a
DefaultRenderController : RenderController
. Which is the same as your controller above.Then add this to your configure services.
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 theDefaultRenderController
.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.is working on a reply...