473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to swapp the default Membership Provider?

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
4 7203
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: =?Utf-8?B?Q2hyaXMgQ2Fw?= | last post by:
I have been having some trouble with implementing a custom Membership Provider. We have a custom data store and business logic that pulls user information. I need some level of functionality...
3
by: Ted | last post by:
In WSAT, I get the following error when trying to set up my provider: Could not establish a connection to the database. If you have not yet created the SQL Server database, exit the Web Site...
4
by: =?Utf-8?B?U2FsYW1FbGlhcw==?= | last post by:
Hi, I am trying to play with the Survey manager application provided gracefully by Microsoft at "http://msdn.microsoft.com/vstudio/express/sql/samples/" VB team(so many thanks), compiled the win...
1
by: Ben | last post by:
Hi, When an anonymous user has created an new account (with the CreateUserWizard control), i want to let asp.net generate a password and to send it to the address of the email provided by the...
4
by: sloan | last post by:
It looks like the default Membership Provider (and Role Provider) always goes to the database to get its info. (GetUsers, GetRoles, etc , etc). I guess I'm going to roll my own, because I am...
2
by: GaryDean | last post by:
My ASP.Net application, that uses the SQL Membership Provider, runs fine on my development box (server2003) as long as I use the standard provider. But, in anticipation of deployment to other...
3
by: GaryDean | last post by:
I have serveral applications now running that are using the MembershipProvider classes and they are each using their own security tables in SQL Server 2005 instead of the express databases - they...
6
by: Jonathan Wood | last post by:
Although this will be a challenge at my level of ASP.NET knowledge, I'm thinking I should implement my own membership provider class. Looking over the methods I must implement, a number of...
0
by: myth0s | last post by:
Hi, The question: Is it possible to have two differents ActiveDirectory Membership Provider in web.config and change at run-time from the default provider to the second provider if the first one...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.