My application has MVC controllers and WebApi controllers. Up to this point, I've been able to push any logic shared between the controllers into a BaseController
and BaseApiController
, respectively.
Now I have a situation where both an API controller and MVC controller are doing the same thing - registering a new user account. This process is a bit complex because I have a multi-tenant application, and looks something like this:
// MVCController : BaseController
public ActionResult Register(RegisterModel model) {
// see if the model is valid
// check and make sure the credentials check out with policy requirements
// add the user to the user table if they aren't already there
// create a tenancy
// assign cookie
}
Most of the actual work is pushed off into the service layer, but the calls and conditions take up about 20 lines of code that I'd rather not copy and paste into the analogous WebApi controller.
So I'm wondering if I can somehow have both my BaseController
and BaseApiController
inherit from a generic controller where I can put this auth code. What's the recommended approach here?