Solved

Membership provider changes after successful login (multiple membership providers)

Posted on 2010-11-08
7
824 Views
Last Modified: 2012-05-10
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?

Thanks

Anubis.
0
Comment
Question by:Anubis2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 250 total points
ID: 34087804
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:

tb_member

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.

0
 
LVL 1

Author Comment

by:Anubis2005
ID: 34088010
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.

Thanks
Anubis.
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 250 total points
ID: 34088454
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.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:Anubis2005
ID: 34088664
Hi guru_sami,

Thanks for the reply.

This is ok for when I manually want to call it, but asp.net 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 asp.net that it should maintain the specified provider.

Thanks
Anubis.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 34088693
-->asp.net 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.
0
 
LVL 1

Accepted Solution

by:
Anubis2005 earned 0 total points
ID: 34159000
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.
Anubis.
0
 
LVL 1

Author Closing Comment

by:Anubis2005
ID: 34186435
Solved the issue myself by writing a custom class to handle what I wanted.  Not a perfect solution, but it's working.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question