By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

How to swapp the default Membership Provider?

P: n/a
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
Oct 14 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <to*@tom.comwrote in message
news:nf*****************@newssvr29.news.prodigy.ne t...
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

Oct 15 '06 #2

P: n/a
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 [Microsoft MVP]" <BA**********@newsgroups.nospamwrote in message
news:eq*************@TK2MSFTNGP05.phx.gbl...
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" <to*@tom.comwrote in message
news:nf*****************@newssvr29.news.prodigy.ne t...
>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


Oct 15 '06 #3

P: n/a
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.ComputerNam e;
} 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
<connectionStringssection, 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
Oct 15 '06 #4

P: n/a
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
Oct 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.