Background
I have a multitenant application that uses multiple databases per-tenant. I'm using CodeFirstMembership, so I have full controll over the SimpleMembership implementation. Both my User/Role entities are in the same DbContext as the rest of my application.
The Problem
In order to facilitate multi-tenance, I have a custom route that looks exactly the same as what the default vanilla MVC route looks like, with the exception that I grab the subdomain, check it against the tenants that have an account, and grab their specific connection string. I have an extension method on the RouteData called .GetSubdomain() that will return the subdomain used, so I can really do the check and get the connection string wherever, if that helps you with your answer.
I need my membership provider to be able to access the subdomain check information in order to point to the correct database for the [Authorize] method to work correctly.
What I've tried
- Initializing the membership provider in the InitializeSimpleMembershipAttribute
- This didn't work because you can't pass in dynamic parameters into attributes (like RouteData.GetSubdomain())
- Initializing the membership in the constructor.
- While you can call RouteData methods in the constructor and have the app build/run, RouteData has not been populated at the point of the constructor in a controller, so this method didn't work either.
- I didn't try this, but adding the check at the start of each controller method is likely not to work since the authorization has already ran.
So... Out of what I've tried, it seems like I need to hook into the point between where RouteData is populated and the actual Authorization. Is there a point I can do this effectively?
Thanks!