我有一个关于多租户(使用MVC3,EF)和自定义SQL成员资格提供程序的问题。
我创建了自定义User和Roles成员资格类,因为我必须在我的应用程序表中包含userId以及其他基于用户的属性。我现在将其转换为多租户应用程序。我们打算使用共享数据库共享应用程序模型。我们有一个主Tenant表,用于存储租户详细信息(包括URL),Tenant_id已包含在每个表中。现在我们正在努力改变应用程序本身。
假设租户将使用如下网址登录: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中设置。
以上是一种很好的方法吗?还是有更好的方法?有人知道我应该注意哪些问题吗?
我已经看到了一个将tenantId存储在AppSettings Handling data access in multi tenant site中的示例。这是一个更好的方法吗?
谢谢
答案 0 :(得分:0)
您的算法非常完美,事实上我一直在从事这种实现。我建议您使用自定义对象来维护不同层的用户身份。这将包含userid,tenantid等..因为这个HttpContext在WCF服务的情况下以及在属于租户并代表另一个租户运行的用户的上下文中将不会帮助您。因此,拥有一个标识用户的UserIdentity对象是一个更好的选择。此外,请将tenantids发送到数据访问层,并且不要从请求中推断租户ID,因为它不会在所有环境中都可用。