Set Membership Provider to use a specific Provider

2019-09-01 14:29发布

问题:

I'm using the out of the box Asp.Net Membership functionality to handle my user management and role management.

My application consists of a single database for each client and each database has its own Membership provider and role provider.

However, there are some users who work for more than one of the clients and I want to be use a single login and let them choose which client to view. I have everything setup to facilitate this "client switching" by using an intermediate database that stores all usernames and a relation to which clients they have access to. I also have it setup so any updates to a user in one database will update all of the corresponding users in the other databases. All of this logic is working very well, except that I can't seem to figure out how to tell ASP.Net to change to a specific MembershipProvider and use that one.

The process I thought I should use is as follows (when the user switches the client in the clients dropdown):

  • Log the current user out
  • Tell ASP.Net which Membership provider to use (selected based on the client chosen in the dropdown)
  • Log the current user in using the selected client's membership provider and refresh/redirect to the page they were viewing when they changed the dropdown

I know how to get a reference to the specific membership provider (Membership.Providers[MembershipProviderName]), but I can't find any information on how to tell ASP.Net to change its membership provider. I'm not really even sure how the asp.net Login control does this in the background either - something that would probably help me out in all of this.

I've been searching SO and the web for awhile and can't seem to find much about doing this other than a few threads where people are trying to modify the DefaultProvider attribute of the Providers element in their Web.config.

回答1:

This MSDN tutorial may help you.