using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; namespace Umbraco.Extensions; public static class ControllerExtensions { /// /// Runs the authentication process /// /// /// public static async Task AuthenticateBackOfficeAsync(this ControllerBase controller) => await controller.HttpContext.AuthenticateBackOfficeAsync(); /// /// Return the controller name from the controller type /// /// /// public static string GetControllerName(Type controllerType) { if (!controllerType.Name.EndsWith("Controller") && !controllerType.Name.EndsWith("Controller`1")) { throw new InvalidOperationException("The controller type " + controllerType + " does not follow conventions, MVC Controller class names must be suffixed with the term 'Controller'"); } return controllerType.Name[..controllerType.Name.LastIndexOf("Controller", StringComparison.Ordinal)]; } /// /// Return the controller name from the controller instance /// /// /// public static string GetControllerName(this Controller controllerInstance) => GetControllerName(controllerInstance.GetType()); /// /// Return the controller name from the controller type /// /// /// /// public static string GetControllerName() => GetControllerName(typeof(T)); }