我已经成功地使用Saaskit在MVC应用程序中实现了多租户。应用程序为每个租户都有一个单独的数据库。我想在Webforms项目中实现类似的功能。谁能指出我正确的方向?可能吗
必须具备:
答案 0 :(得分:0)
设法使用Webforms 4.7.2中新的Unity支持来支持此功能:
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)
{
}
}
示例数据库上下文,其中包含每个租户的数据库
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");
}
}
答案 1 :(得分:-1)
默认成员资格API应该满足要求。
如果没有,请考虑使用http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider
参考教程> http://www.asp.net/security/tutorials
视频http://www.asp.net/security/videos
上面的教程中介绍了最佳做法。