MVC 多租户加载其他租户数据
MVC Multi tenant loads other tenant data
我正在使用 ASP.NET MVC 构建多租户应用程序。现在,注意到一个错误,但在完全随机的时间间隔内,有时会为另一个租户获取错误租户的数据。
例如,租户 1 登录,但他们看到来自租户 2 的信息。我使用来自所有租户的相同数据库,但使用 TenantId。
我从 Global > Application_AcquireRequestState 启动应用程序,如下所示:
namespace MultiTenantV1
{
public class Global : HttpApplication
{
public static OrganizationGlobal Tenant;
void Application_Start(object sender, EventArgs e)
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityWebActivator.Start();
}
void Application_AcquireRequestState(object sender, EventArgs e)
{
// boot application
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// dependency runner
Tenant = new Tenant().Fetch(context.Request.Url);
// third party api calls
var session = HttpContext.Current.Session;
if (session != null)
{
if (string.IsNullOrEmpty(Session["CountryCode"] as string))
{
string isDevelopmentMode = ConfigurationManager.AppSettings["developmentmode"];
if (isDevelopmentMode == "false")
{
// api call to get country
}
else
{
// defaults
}
}
Tenant.CountryCode = Session["CountryCode"].ToString();
}
}
}
}
现在,在整个应用程序中,我使用 'Tenant' 对象作为起点,并使用它来查询数据库以获取更多数据。我注意到,有时一个租户会看到另一个租户名称(不确定其他数据是否也以同样的方式可见)。
我正在根据 HttpContext.Request.Url 初始化 'Tenant'。所以没办法加载其他租户的数据。
任何人都可以在上面的代码中看到任何内容,或者在我使用 HttpContext.Request.Url 时可能会导致为任何特定请求提取错误的租户吗?
每个请求都会覆盖静态租户对象,因此在并发请求中,将使用错误的租户。
关键是根据请求存储租户,例如在 HttpContext.Current
中。我通常使用租赁解析器,其中包含如下代码:
public string CurrentId {
get {
return (string)HttpContext.Current.Items["CurrentTenantId"];
}
set {
string val = value != null ? value.ToLower() : null;
if (!HttpContext.Current.Items.Contains("CurrentTenantId")) {
HttpContext.Current.Items.Add("CurrentTenantId", val);
} else {
HttpContext.Current.Items["CurrentTenantId"] = val;
}
}
}
在Application_AcquireRequestState
中我根据url设置了CurrentId
。
然后在需要了解租户的 类 中使用租户解析器,方法是获取 CurrentId
。
我正在使用 ASP.NET MVC 构建多租户应用程序。现在,注意到一个错误,但在完全随机的时间间隔内,有时会为另一个租户获取错误租户的数据。
例如,租户 1 登录,但他们看到来自租户 2 的信息。我使用来自所有租户的相同数据库,但使用 TenantId。
我从 Global > Application_AcquireRequestState 启动应用程序,如下所示:
namespace MultiTenantV1
{
public class Global : HttpApplication
{
public static OrganizationGlobal Tenant;
void Application_Start(object sender, EventArgs e)
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityWebActivator.Start();
}
void Application_AcquireRequestState(object sender, EventArgs e)
{
// boot application
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
// dependency runner
Tenant = new Tenant().Fetch(context.Request.Url);
// third party api calls
var session = HttpContext.Current.Session;
if (session != null)
{
if (string.IsNullOrEmpty(Session["CountryCode"] as string))
{
string isDevelopmentMode = ConfigurationManager.AppSettings["developmentmode"];
if (isDevelopmentMode == "false")
{
// api call to get country
}
else
{
// defaults
}
}
Tenant.CountryCode = Session["CountryCode"].ToString();
}
}
}
}
现在,在整个应用程序中,我使用 'Tenant' 对象作为起点,并使用它来查询数据库以获取更多数据。我注意到,有时一个租户会看到另一个租户名称(不确定其他数据是否也以同样的方式可见)。
我正在根据 HttpContext.Request.Url 初始化 'Tenant'。所以没办法加载其他租户的数据。
任何人都可以在上面的代码中看到任何内容,或者在我使用 HttpContext.Request.Url 时可能会导致为任何特定请求提取错误的租户吗?
每个请求都会覆盖静态租户对象,因此在并发请求中,将使用错误的租户。
关键是根据请求存储租户,例如在 HttpContext.Current
中。我通常使用租赁解析器,其中包含如下代码:
public string CurrentId {
get {
return (string)HttpContext.Current.Items["CurrentTenantId"];
}
set {
string val = value != null ? value.ToLower() : null;
if (!HttpContext.Current.Items.Contains("CurrentTenantId")) {
HttpContext.Current.Items.Add("CurrentTenantId", val);
} else {
HttpContext.Current.Items["CurrentTenantId"] = val;
}
}
}
在Application_AcquireRequestState
中我根据url设置了CurrentId
。
然后在需要了解租户的 类 中使用租户解析器,方法是获取 CurrentId
。