views:

234

answers:

1

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?

+2  A: 

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

related questions