ASP.Net MVC for each membership area

I am creating an ASP.Net MVC application that will run in a shared hosting account to host multiple domains. I started with a default template that includes membership and created an mvc scope for each domain. Routing is configured to point to the correct scope depending on the domain for which the request is required. Now I would like to set up membership in each area of ​​mvc. At first I tried to make it obvious and tried to override the web.config section for each scope in order to change the provider applicationName attribute. This does not work because the scope is not configured as the root of the application. Is there an easy way to split users for each area?

+3
source share
1 answer

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 the string (also modified existing constructors to disambiguate).

    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 to each area and changed the Initialize overload to include the name of the area 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 must be stored separately.

+2
source

Source: https://habr.com/ru/post/1743564/


All Articles