1

I have a scenario in which my application connects to different databases with respect to subdomains in url.

I have tried this code:

 public class Provider : 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.

        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder["Data Source"] = "My-PC\\SQLEXPRESS";
        string dbName;
        if (!string.IsNullOrEmpty(Helpers.RouteManager.GetSubDomain()))
        {
            dbName = Helpers.RouteManager.GetSubDomain().ToString()+"_db";
        }
        else
        {
            dbName = "dbName";
        }
        bool isEqual = string.Equals(name, dbName);
        sqlBuilder["Initial Catalog"] = dbName;
        sqlBuilder["Integrated Security"] = true;
        sqlBuilder["Application Name"] = "EntityFramework";
        FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
        connectionStringField.SetValue(this, sqlBuilder.ConnectionString);
    }
}

And I am calling this function from Application_BeginRequest() method.

The issue I am facing is that when I build application and it hits url with specific sub-domain, it connects to the specified database but I try different sub-domain it sticks with the same database for asp.net membership.

But when i rebuild the application and try another sub domain it works.

It seems that my code is working only for the first time after I build the solution.

Please someone guide me.

Thanx

bit
  • 4,407
  • 1
  • 28
  • 50
user2517610
  • 275
  • 2
  • 8
  • 27

2 Answers2

1

Try to configure different Membership providers in web config file.

<membership>
    <providers>
        <add name="ProviderA" connectionStringName="ConnectionA" ... />
        <add name="ProviderB" connectionStringName="ConnectionB" ... />
    </providers>
</membership>

<connectionStrings>
     <add name="ConnectionA" .... />
     <add name="ConnectionB" .... />
</connectionStrings>

And then use this providers in code

var user = Membership.Providers["ProviderA"].GetUser(username, false);
var user = Membership.Providers["ProviderB"].GetUser(username, false);
Pavel
  • 526
  • 2
  • 5
  • I do not want to add providers and connection string manually in web.config. Is there any dynamic approach? Thanx – user2517610 Nov 10 '14 at 12:17
0

sorry this isn't really an answer but I'd like to question your architecture here. Why do you need your application to contain membership data in multiple databases? It's nature is to maintain account details (authentication/authorization) for your application as a whole. If you want the ability to control access to certain areas of your application based on the Url you could do this by using Role based Forms Authentication. There are lots of articles on the interweb explaining this. Hope this sets you off on the right direction.

Update: I've just found this answer on stackoverflow. I'm afraid it seems what you want to do is not possible as the membership provider is a global static object shared between all users of your application. Changing it based on a url would not just change it for that particular user but for everyone that is logged in check it out here

Community
  • 1
  • 1
Simon Ryan
  • 212
  • 1
  • 9
  • I am creating multi-tenant application with separate databases approach to isolate data of one tenant's users from another tenant. – user2517610 Nov 10 '14 at 11:15
  • Have you put a breakpoint in the method to see when it gets hit? From my understanding the Initialise method is triggered when the provider is instantiated (so only once). It isn't supposed to be called directly. You can however instantiate a provider manually which will trigger initialise `MyCustomMembershipProvider myProvider = (MyCustomMembershipProvider)Membership.Providers["NameOfMembershipProviderInConfig"];` – Simon Ryan Nov 10 '14 at 11:58
  • Did you have a look at my updated answer? It seems it is not possible to do what you are trying to do. The membership provider object is static so cannot be changed for each user. It is shared between all users. – Simon Ryan Nov 10 '14 at 14:08
  • Yes I did, Just waiting if some one have solution. . .if not I will be accept your answer. Thanx – user2517610 Nov 11 '14 at 04:08