2012-01-03 2 views
0

これは私の問題です。私は複数の顧客のために1つだけのウェブサイトを持っています。自分のデータにアクセスするには、顧客がこのようなURLを使用します。 http://customer1.mysite.com http://customer2.mysite.com などASP.NET複数の潜在的に不明なSQL Serverインスタンスの1つのWebサイトのメンバシップ

各顧客が自分のデータとのSQLServerのインスタンスを持っています。

URLに応じて、Webサイトは正しいSQL Serverインスタンスに接続します。これはいい。

私の問題はメンバーシップに関するもので、各インスタンスは独自の「メンバーシップデータベース」です。また

<membership defaultProvider="MyMembershipProvider"> 
<providers> 
<clear /> 
<add name="MyMembershipProvider" 
type="MapApp.MyMembershipProvider, MyApp"    
enablePasswordRetrieval="true" 
enablePasswordReset="true" 
requiresQuestionAndAnswer="false" 
applicationName="/" 
requiresUniqueEmail="false" 
passwordFormat="Encrypted" 
minRequiredPasswordLength="5" 
minRequiredNonalphanumericCharacters="0" 
passwordAttemptWindow="10" 
passwordStrengthRegularExpression="" 
connectionStringName="A_DATABASE" /> 
</providers>   
</membership> 

が、私はこのようなコードでカスタムのMembershipProviderあります:

public class MyMembershipProvider : SqlMembershipProvider 
{ 
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) 
{ 
    base.Initialize(name, config); 
    // Update the private connection string field in the base class. 
    string connectionString = "my new connection string depdend of the customer" 
    // Set private property of Membership provider. 
    FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); 
        connectionStringField.SetValue(this, connectionString); 
} 
} 

のが、「すでにインスタンス化」のようないくつかの問題を持って十分ではありません私のWebConfigで私はこのようなダミーセクションを設定します私は私のカスタムメンバーシッププロバイダーに電話します。

誰かが私を助けることができますか?してください...

この長い記事で私の悪い英語のため申し訳ありません

答えて

0

私は解決策があると思いますが、それは少し奇妙です。

私のcustomMemberShipProviderクラスでは、決して呼び出さなくても、空のコンストラクタが必要です。そして私は別のコンストラクタを必要とします(たとえ私がそれを使っていなくても)。

だからこれは私のコードです:

public class myMembershipProvider : SqlMembershipProvider 
{ 
    public myMembershipProvider() 
    { 
     //never use 
    } 

    public myMembershipProvider(string dummyArg) 
    { 
     string configPath = "~/web.config"; 
     Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); 
     MembershipSection section = (MembershipSection)config.GetSection("system.web/membership"); 
     ProviderSettingsCollection settings = section.Providers; 
     NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters; 
     if ((HttpContext.Current.Session != null) && (HttpContext.Current.Session["SubDomainInstanceName"] != null) && (!string.IsNullOrEmpty(HttpContext.Current.Session["SubDomainInstanceName"].ToString()))) 
     { 
      membershipParams.Add("Instance", HttpContext.Current.Session["SubDomainInstanceName"].ToString()); 
     } 
     else 
     { 
      membershipParams.Add("Instance", "default"); 
     } 
     Initialize(section.DefaultProvider, membershipParams); 
    } 


    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) 
    { 
     //need to remove the key before call Initialize method 
     string instance = config["Instance"]; 
     config.Remove("Instance"); 
     base.Initialize(name, config); 

     // Update the private connection string field in the base class. 
     string connectionString = //my specific connectionstring; 
     // Set private property of Membership provider. 
     FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); 
     connectionStringField.SetValue(this, connectionString); 
    } 

よろしく

0

これは興味深い問題です。 URLに基​​づいて、データベースに別の接続文字列を使用しようとしているようです。これはやや優雅ではないかもしれませんが、SQLメンバーシップ・プロバイダのコードを取得し、使用する接続文字列がユーザーURLに基​​づいて変更されるようにしてください。私はあなたが本当に1つのasp.netアプリケーションを使用している場合、これは良い仕事の周りになると思う。

関連する問題