Handling Multi Tenancy on MVC

半腔热情 提交于 2019-12-01 02:20:20

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!