I am building an ASP.Net MVC app that will run on a shared hosting account to host multiple domains. I started with the default template that includes membership and created an mvc area for each domain. Routing is set up to point to the correct area depending on the domain the request is for. Now I would like to set up membership specific to each mvc area. I tried the obvious first and attempted to override the section of the web.config for each area to change the applicationName attribute of the provider. That doesn't work since the area is not set up as an application root. Is there an easy way to separate the users for each area?
Asked
Active
Viewed 1,450 times
3

AdmSteck
- 1,753
- 15
- 25
-
1I'm a little curious why you are leveraging areas to do this to begin with. That's not really the normal use case for them. – Mallioch Apr 30 '10 at 19:51
-
What you are describing is a Multi-Tenant application. Do a search for that term, and you will find several examples. – Robert Harvey Apr 30 '10 at 19:53
-
1@Mallioch I'm doing this because I'm cheap and I like clean urls. :) I have one hosting account but maintain my own personal domain as well as one for my church and I am starting one for my wife's photography. This setup allows me to keep each domain as a logically separate mvc area, maintain clean urls for each, while still only using one hosting account and one sql database. – AdmSteck Apr 30 '10 at 20:03
-
1normal use cases ride rainbow unicorns. – Sky Sanders Apr 30 '10 at 20:33
1 Answers
2
I think I have a working solution that keeps each area completely separate. Using the default template as a starting point I added another constructor to the MvcApplication1.Models.AccountMembershipService class to accept a string (Also modified the existing constructors to eliminate ambiguity).
public AccountMembershipService()
{
_provider = Membership.Provider;
}
public AccountMembershipService(MembershipProvider provider)
{
_provider = provider ?? Membership.Provider;
}
public AccountMembershipService(string applicationName)
: this()
{
_provider.ApplicationName = applicationName;
}
Then I copied the AccountController into each area and modified the Initialize overload to include the area name from the route data.
protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
if (MembershipService == null) { MembershipService = new AccountMembershipService(requestContext.RouteData.DataTokens["area"].ToString()); }
base.Initialize(requestContext);
}
Now each area is registered as a new application under forms authentication and all users and roles should be kept separate.

AdmSteck
- 1,753
- 15
- 25