How to swapp the default Membership Provider?

T

thomas

Hello All,

How to change the default Membership Provider during the runtime?

I know I can reference any provider I want, e.g.: provider =
Membership.Providers["MyMembershipProvider"]
but the question is how to change the default one, so all those new, cool
controls can start using the one I want.

I can specify the provider for each of those controls, e.g.:
Login1.MembershipProvider =
Membership.Providers["DefaultMembershipProvider"].Name
but it seems like before this line in the Page_Load event handler gets
executes ASP.Net tries to initialize the default one, and since, the
connection string is not always valid, an exception occurs.

I guess, alternatively, I could change the connection string of the default
provider, but I do not know how to.

Any help highly appreciated.

Tomasz
 
K

Ken Cox [Microsoft MVP]

Hi Tomasz

You need to make sure all the potential membership providers are listed in
the web.config file, like this:

<membership defaultProvider="Mydefaultprovider">
<providers >
<clear/>
<add connectionStringName="ASPNETDBConnectionString1"
name="MyOracleprovider"
type="System.Web.Security.OracleMembershipProvider "/>
<add connectionStringName="ASPNETDBConnectionString2"
name="Mydefaultprovider" type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>

Then in your code, reference the one you want to use:

Dim mbr As MembershipProvider
mbr = Membership.Providers.Item("MyOracleprovider")
' mbr.CreateUser(....
Response.Write(mbr.GetType)

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
 
T

thomas

Hi Ken,

I know that this seems like a typical question many people ask.
That is why I stated in my post that I knew how to reverence an existing
provider knowing that someone may attempt to help without actually reading
the problem description.
But anyway, thank you.

Tomasz



Ken Cox said:
Hi Tomasz

You need to make sure all the potential membership providers are listed in
the web.config file, like this:

<membership defaultProvider="Mydefaultprovider">
<providers >
<clear/>
<add connectionStringName="ASPNETDBConnectionString1"
name="MyOracleprovider"
type="System.Web.Security.OracleMembershipProvider "/>
<add connectionStringName="ASPNETDBConnectionString2"
name="Mydefaultprovider"
type="System.Web.Security.SqlMembershipProvider"/>
</providers>
</membership>

Then in your code, reference the one you want to use:

Dim mbr As MembershipProvider
mbr = Membership.Providers.Item("MyOracleprovider")
' mbr.CreateUser(....
Response.Write(mbr.GetType)

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

thomas said:
Hello All,

How to change the default Membership Provider during the runtime?

I know I can reference any provider I want, e.g.: provider =
Membership.Providers["MyMembershipProvider"]
but the question is how to change the default one, so all those new, cool
controls can start using the one I want.

I can specify the provider for each of those controls, e.g.:
Login1.MembershipProvider =
Membership.Providers["DefaultMembershipProvider"].Name
but it seems like before this line in the Page_Load event handler gets
executes ASP.Net tries to initialize the default one, and since, the
connection string is not always valid, an exception occurs.

I guess, alternatively, I could change the connection string of the
default provider, but I do not know how to.

Any help highly appreciated.

Tomasz
 
T

thomas

Here is the solution I came up with: create own MembershipProvider.
Sounds like a lot of work, doesn't it? Well, actually it does not have to be
created from scratch.
How about altering slightly the behavior of the existing
SqlMembershipProvider, so instead of using the predefined connection string
it grabs the one I specify dynamically, in my case based on the host name?
Here is how simple it is:

class MyMembershipProvider : SqlMembershipProvider
{
public override void Initialize(string name, NameValueCollection config)
{
config["connectionStringName"] = ConnectionString.Name;
base.Initialize(name, config);
}
}

And for the completeness:

public static string ComputerName {
get {
if (System.Web.HttpContext.Current == null) {
return System.Windows.Forms.SystemInformation.ComputerName;
} else {
return System.Web.HttpContext.Current.Server.MachineName;
}
}
}

public static ConnectionStringSettings ConnectionString {
get {
ConnectionStringSettings connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings[ComputerName+
"ConnectionString"];
if (connectionString == null) {
connectionString =
ConfigurationManager.ConnectionStrings["DefaultConnectionString"];
}
return connectionString;
}
}

Now I can define several connection strings in the config
<connectionStrings> section, each for the host/environment where I plan to
install my application, and one default MembershipProvider of the
MyMembershipProvider type with empty connection string. Example:

<connectionStrings>
<add name="SomeHost1ConnectionString" connectionString="..."/>
<add name="SomeHost2ConnectionString" connectionString="..."/>
<add name="DefaultConnectionString" connectionString="..."/>
</connectionStrings>

<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="MyMembershipProvider"
connectionStringName="" applicationName="MyApplication"/>
</providers>
</membership>

Now, when the time comes and ASP.Net initializes my customized default
provider the provider will choose the right connection string itself.
The solution can be further extended, but it perfectly works for me as it
is, so I decided to post it here.

Tomasz
 
T

thomas

One more comment: this method does not actually allows to swap the provider
However, I found that what I, and the most of those who ask this question,
really needed was to be able to make the default MambershipProvider using a
particular connection string, rather than to be able to change the provider
type anytime.

Tomasz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top