Handling Multi Tenancy on MVC

前端 未结 1 461
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 09:27

I would like to ask your opinions regarding the way I handle MultiTenancy. I\"m using MVC3 (switching to MVC4) and EF as my backend. I\'m using a single app, shared schema M

相关标签:
1条回答
  • 2021-01-07 09:46

    We are building pretty big multi tenancy web app at the moment. Well it's not so simple as it seems but once you build your architecture it's straight forward. Our is deeply in development but you can check out the open source part in nanocms.codeplex.com (we haven't uploaded the db jet butt we will in few days)

    Since this is a pretty wide question I'll try to summarize some problems and solutions.

    First you need to identify tenant for each request. We have a global action filter that parses url and compares it with data in database. Of course you must cache all data so no calls to database are made. You must not save any of that in a cookie or session because user can visit more than one tenant at the time. I suggest that you put that data in HttpRequest Items so you do that only once in a request but still have that data available always.

    For authenticate users must be unique. You must think if you want to give some user different rights on each tenant. If so you must write your authenticate code and even attribute so you can check his role in current tenant. In our app when user authenticates we create session object. In object we have static methods that check for rights in tenant.

    I suggest you keep HttpRequest Items strongly typed. We have:

    public static int TenantID {
        get { return System.Web.HttpContext.Current.Items.Contains("TenantID") ? Convert.ToInt32(System.Web.HttpContext.Current.Items["TenantID"]) : -1; }
        set {
            if (!System.Web.HttpContext.Current.Items.Contains("TenantID"))
                System.Web.HttpContext.Current.Items.Add("TenantID", value);
            System.Web.HttpContext.Current.Items["TenantID"] = value;
        }
    }
    
    0 讨论(0)
提交回复
热议问题