2020-04-20 12:20:47 +02:00
using System ;
2020-12-02 15:49:28 +11:00
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Authentication ;
2020-04-20 12:20:47 +02:00
using Microsoft.AspNetCore.Mvc ;
2020-05-07 10:08:23 +02:00
namespace Umbraco.Extensions
2020-04-20 12:20:47 +02:00
{
public static class ControllerExtensions
{
2020-12-02 15:49:28 +11:00
/// <summary>
2021-02-18 11:06:02 +01:00
/// Runs the authentication process
2020-12-02 15:49:28 +11:00
/// </summary>
/// <param name="controller"></param>
/// <returns></returns>
public static async Task < AuthenticateResult > AuthenticateBackOfficeAsync ( this ControllerBase controller )
{
if ( controller . HttpContext = = null )
{
return AuthenticateResult . NoResult ( ) ;
}
2021-02-18 11:06:02 +01:00
var result = await controller . HttpContext . AuthenticateAsync ( Cms . Core . Constants . Security . BackOfficeAuthenticationType ) ;
2020-12-02 15:49:28 +11:00
return result ;
}
2020-04-20 12:20:47 +02:00
/// <summary>
/// Return the controller name from the controller type
/// </summary>
/// <param name="controllerType"></param>
/// <returns></returns>
2020-05-13 14:49:00 +10:00
public static string GetControllerName ( Type controllerType )
2020-04-20 12:20:47 +02:00
{
if ( ! controllerType . Name . EndsWith ( "Controller" ) )
{
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 . Substring ( 0 , controllerType . Name . LastIndexOf ( "Controller" ) ) ;
}
/// <summary>
/// Return the controller name from the controller instance
/// </summary>
/// <param name="controllerInstance"></param>
/// <returns></returns>
2020-05-13 14:49:00 +10:00
public static string GetControllerName ( this Controller controllerInstance )
2020-04-20 12:20:47 +02:00
{
return GetControllerName ( controllerInstance . GetType ( ) ) ;
}
/// <summary>
/// Return the controller name from the controller type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <remarks></remarks>
2020-05-13 14:49:00 +10:00
public static string GetControllerName < T > ( )
2020-04-20 12:20:47 +02:00
{
return GetControllerName ( typeof ( T ) ) ;
}
}
}