我正在嘗試使用多租戶ASP.NET MVC應用程序遷移到Azure(包括SQL Azure)。每個客戶都獲得自己的數據庫,這是自包含的,包括他們所有的會員資格證書。多租戶SQLMembershipProvider ASP.NET MVC
我們可以在SqlMembershipProvider對象初始化時將連接字符串設置爲SqlMembershipProvider。然而,對不同子域(在同一會話中)的後續請求不會更改連接字符串。我發現了一個例子,其中的實現覆蓋了SqlMembershipProviders的ConnectionString,但這在System.Web dll的4.0版本中是不可能的。
我們可以實現一個單一的會員資格數據庫並進行身份驗證......但我們希望在這個SAAS模型中保留客戶證書。
所以問題是如何爲每個請求動態地更改SQLMembershipProviders連接字符串?
的Web.config
<membership defaultProvider="TenantMembershipProvider">
<providers>
<clear/>
<add name="TenantMembershipProvider" type="ABC.Infrastructure.MultiTenancy.TenantMembershipProvider, ABC"
connectionStringName="ApplicationServices" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false"
requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>
</providers>
</membership>
TenantMembershipProvider.cs處理該初始化
public class TenantMembershipProvider : SqlMembershipProvider
{
private SiteLinqSession _session;
private MasterSession _masterSession;
private static readonly Dictionary<string, Customer> _customers = new Dictionary<string, Customer>();
private static string _host;
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);
string connectionString = GetConnectionString();
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, connectionString);
}
private string GetConnectionString()
{
var headers = HttpContext.Current.Request.Headers["Host"];
string[] host = headers.Split('.');
_host = host[0];
if (_host == "127") _host = "demo";
var customer = GetSite(_host);
return BuildTenantConnectionString(customer.ConnectionSetting);
}
private Customer GetSite(string host)
{
Customer customer;
//check dictionary if customer exists for the subdomain
_customers.TryGetValue(host, out customer);
if (customer != null)
return customer;
//if not get the customer record and add it to the dictionary
_masterSession = new MasterSession();
var customers = _masterSession.All<Customer>();
customer = customers.SingleOrDefault(x => x.SubDomain == _host);
if (customer != null)
_customers.Add(host, customer);
return customer;
}
private string BuildTenantConnectionString(ConnectionSetting setting)
{
return string.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", setting.DataSource, setting.Catalog, setting.Username, setting.Password);
}
}
謝謝我將在上面發佈的鏈接中查看asp.net提供程序工具包SQL示例。 – Diesel337 2011-12-15 19:47:59
確定工具包使用的是.NET框架的2.0版本,它看起來像訪問一些azure庫可能存在一些問題[Windows Azure是否與.NET 2.0框架兼容?](http://stackoverflow.com/questions-6442443/is-windows-azure-compatible-the-net-2-0-framework) – Diesel337 2011-12-15 20:04:56