我在使用Saaskit实现MVC应用程序中的多租户方面取得了成功。对于每个租户,应用程序都有一个单独的数据库。我想在webforms项目中实现类似的东西。谁能给我指明正确的方向?有可能吗?
必须具备:
发布于 2019-05-03 13:54:31
设法使用Webforms 4.7.2中的新的统一支持来支持这一点:
public class TenantResolver : ITenantResolver
{
public Tenant GetTenant()
{
var identifier = HttpContext.Current.Request.Url.Host.ToLower();
return AllTenants().FirstOrDefault(x => x.HostNames.Any(a => a.Hostname.Contains(identifier)));
}
public List<Tenant> AllTenants()
{
// return list of tenants from configuration or seperate db
}
}启动中的
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var container = this.AddUnity();
container.RegisterType<ITenantResolver, TenantResolver>();
container.RegisterType<ApplicationContext, ApplicationContext>();
}具有租户访问权限的示例页
public partial class About : Page
{
readonly Tenant tenant;
readonly ApplicationContext _context;
public About(ITenantResolver tenantresolver, ApplicationContext context)
{
tenant = tenantresolver.GetTenant();
_context = context;
}
protected void Page_Load(object sender, EventArgs e)
{
}
}示例db上下文与每个租户中的db
public class ApplicationContext : DbContext
{
public ApplicationContext(ITenantResolver tenantResolver) : base(ConnectionStringResolver(tenantResolver)) {
}
private string ConnectionStringResolver(AppTenant appTenant)
{
var tenant = tenantResolver.GetTenant();
if (tenant != null)
{
return tenant.ConnectionString;
}
throw new NullReferenceException("Tenant Not Found");
}
}发布于 2019-04-04 09:33:24
默认的成员资格API应该能够满足需求。
如果没有,请考虑http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider
请参阅tutorials> http://www.asp.net/security/tutorials
视频http://www.asp.net/security/videos
最佳实践将在上面的教程中解释。
https://stackoverflow.com/questions/55512466
复制相似问题