【发布时间】:2013-04-29 02:23:03
【问题描述】:
我有一个关于多租户(使用 MVC3、EF)和自定义 SQL 成员资格提供程序的问题。
我创建了自定义用户和角色成员资格类,因为我必须在我的应用程序表中包含 userId 以及其他基于用户的属性。我现在将其转换为多租户应用程序。我们正在寻找一个共享数据库、共享应用程序模型。我们有一个存储租户详细信息(包括 URL)的主租户表,并且 Tenant_id 已包含在每个表中。现在我们正在对应用程序本身进行更改。
假设租户将使用如下 URL 登录:tenantUrl.mybaseURL.com。
我将根据此算法更改我的自定义用户验证方法:
- User goes to URL to login
- The Account controller logon method calls the Custom validate method passing in user/pwd and the tenantId (which has been looked up from the db using the URL).
- The custom validate method checks if the user/password/tenantId combination are valid.
- If valid, we set the tenant Id in a HttpRequest Object and login the user
- For each db access, we lookup the Request object for the tenandId and use that as a filter.
编辑:这是我的 TenantContext 类,我将在其中设置/获取tenantId
public class TenantContext
{
public static int TenantId
{
set
{
if (!HttpContext.Current.Items.Contains("tenant-code"))
HttpContext.Current.Items.Add("tenant-code", value);
HttpContext.Current.Items["tenant-code"] = value;
}
get {return HttpContext.Current.Items.Contains("tenant-code") ? Convert.ToInt32(HttpContext.Current.Items["tenant-code"]) : -1; }
}
}
tenantId 将在上述 Context 中的 Account Controller Login 中设置。
以上是实现此目的的好方法还是有更好的方法?有人看到我应该注意的任何问题吗?
我在Handling data access in multi tenant site 看到了一个将tenantId 存储在AppSettings 中的示例。这是更好的方法吗?
谢谢
【问题讨论】:
-
您是否在询问如何为登录操作的其余部分获取tenantId?或者您是否在询问如何为登录用户发出的后续请求获取tenantId。我问是因为你要在每个后续请求的 HttpContext.Items 中设置tenantId,你必须从某个地方获取它,对吧?
-
嗨,Amith,我在问上面的算法是否正确,因为这是我的第一个多租户应用程序。如果用户有效,我将根据 URl 从数据库中获取租户 ID 并存储在请求对象中。
-
你有没有机会发布你所做的解决方案?
标签: asp.net-mvc-3 multi-tenant custom-membershipprovider