My controllers share a client id. Route:
clients/{clientId}/{controller}/{action}/{id}
Sample URLs:
clients/1/orders/details/1
clients/2/children/index
clients/2/cars/create
You need proper authorization for a client. I don't want to do the same client authorization in every controller. I came up with the idea to do the autorization in a custom controller factory like this:
public class CustomControllerFactory : DefaultControllerFactory
{
private readonly IAuthService _authService;
public CustomControllerFactory(IAuthService authService)
{
_authService = authService;
}
protected override IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
{
var doAuth = requestContext.RouteData.Values.ContainsKey("clientId");
if (doAuth)
{
var principal = requestContext.HttpContext.User;
var clientId = long.Parse(
requestContext.RouteData.Values["clientId"].ToString());
var authorized = _authService.Client(principal, clientId);
if (!authorized)
{
return new AuthController();
}
}
return base.GetControllerInstance(requestContext, controllerType);
}
}
Do you consider this a good practice or not? Why?