Membership provider changes after successful login (multiple membership providers)

Hello All,

I have a site in which I'm intending to use multiple membership providers.  I'm using my own custom membership provider to provide access to several data stores depending on the section in which the user's information resides.

The actual login functions correctly using the built-in ASP.NET Login component in which I've manually specified the membership provider it should validate against.  When I run a trace on it's actions, I can see that it attempts the 'validateuser' against the correct data store and continues to redirect the user to their applicable section.

The problem is that once they are logged in and the site (or I) call 'GetUser' to get the users information or permissions, the membership provider reverts to the site's default provider.

In my custom membership provider I'm only overriding the methods for GetUser and Validate user which is working perfectly fine for a singular membership provider, but not when using a different provider.

How can I ensure that the correct membership provider is retained with the user's membership details?  Is there a method that I need to override/append in order to achieve what I'm looking for?


Who is Participating?
Anubis2005Connect With a Mentor Author Commented:
Hi guru_sami,

It matters when in the page you try to call some information about the authenticated user through the 'Membership.????' class.  This uses the default membership provider for the site as a whole.

I've since resolved my issue by writing my own 'Membership' helper class to use instead of the built-in membership class so that I can specify the provider I should be look in.  

I would have thought that .net would have been smart enough to 'remember' that if a user logs in from a specific membership provider, it would us this provider for all membership related calls beyond that point, but alas, it is not.

Thanks for all your help and comments.
Daniel Van Der WerkenConnect With a Mentor Independent ConsultantCommented:
The only way I can think of to do this right off the bat is to have some way to lookup the membership provider to use in the database.

For example, have a table that correlates the member_id with the provider.

So let's say we have:


Member_id Name  Provider
1                    Anubis  0
2                    Daniel 1

Then, the GetUser( string username, bool userIsOnline )
now does this:

providerName = GetProvider( username );

switch (providerName )

case  FirstMembershipProvider:
  provider = new FirstMembershipProvider();

case SecondMembershipProvider:
  provider = new SecondMembershipProvider();

MembershipUser user = provider.GetUser( username );

Or something like that.

Anubis2005Author Commented:
Hi Dan7el,

Thanks for the reply.  Although this might work, in a case where usernames are the same across multiple providers, it wouldn't work properly.

I've been looking at the methods/steps behind the scenes of how the login component authenticates a user, I'm successfully overriding the 'ValidateUser' part of this, but obviously .NET creates a MembershipUser object for the logged in user at some point.

If I can find at what exact point this is done, I could override it to ensure that my membership user maintains the appropriate provider.

I've been searching for 'behind the scenes' details of the login control's actions but I'm coming up empty.

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

guru_samiConnect With a Mentor Commented:
I don't know to override defaultprovider but you can always select which provider to use like:

Membership.Providers["yourprovidername"].GetUser("someusername", false);
yourprovidername and someusername can be passed via some variable.
Anubis2005Author Commented:
Hi guru_sami,

Thanks for the reply.

This is ok for when I manually want to call it, but when redirecting to protected pages it's calling the default provider instead of the correct provider and throws the user out.

I can't seem to find a way to tell that it should maintain the specified provider.

--> when redirecting to protected pages it's calling the default provider
I didn't get that part...because once you set forms authentication cookie, it shouldn't matter unless you are talking of roles and roles provider.
Anubis2005Author Commented:
Solved the issue myself by writing a custom class to handle what I wanted.  Not a perfect solution, but it's working.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.