Go to SQL Membership Provider from AD Membership Provider Runtime

In my asp.net application administration function, I'm trying to combine AD authentication and form authorization to create users, roles and assign users to roles, etc. I configured MembershipADProvider and AspNetSqlMembershipProvider in my web.config with MembershipADProvider as the default one. After the user logs in using AD authentication, I need to switch / assign the membership object to use AspNetSqlMembershipProvider to get all users from the membership object (from the dbo.aspnet_Users table). How to switch provider at runtime? I tried different approaches after searching for this problem, and none of this has worked for me so far. Here are some approaches I tried: 1. foreach (MembershipProvider mp in Membership.Providers) {if (mp.Name == "MembershipADProvider") {Membership.Providers.Remove (MembershipADProvider ");
MembershipUserCollection users = Membership.GetAllUsers (); ddlUsers.DataSource = users; ddlUsers.DataBind ();
break; }}

Membership.Providers.Remove (MembershipADProvider); - does not work because it is not supported. In addition, I tried to clear Membership.Providers, and then add only the AspNetSqlMembershipProvider type, which are also not supported.

  • I cannot set Memberhip.Provider with a value from Membership.Providers ["AspNetSqlMembershipProvider"] as a membership .Provider is a read-only property.

  • I tried to connect a connection string between two providers that did not use a provider, since both of them are different types of providers. If both were sqlserver providers, this would work, I suppose.

Please let me know if anyone has been successfully implemented or in general is a plausible approach. Thanks!

+4
source share
2 answers

You will pass an explicit provider to your code, and not directly depend on Memebership (which simply wraps the flag marked as default in config). There is no need to change them during operation, consider how this will affect thread safety.

Therefore, instead of saying Membership.GetAllUsers(); , you would do something like (I don't have a compiler):

 public UserSerivce : IUserService { private MembershipProvider provider; public UserService(MembershipProvider provider) { this.provider = provider; } public IEnumerable<MembershipUser> GetUsers() { return provider.GetAllUsers(); } public void DoSomethingElseUseful() { ... } } 

And then use it for a specific provider:

 var service = new UserService(Membership.Providers["mySqlMembershipProvider"]); var users = service.GetUsers(); 

Or, if you use special AD code:

 var service = new UserService(Membership.Providers["myADMembershipProvider"]); var users = service.GetUsers(); 

Using DI in this way also helps keep code in check.

+2
source

If you need a list of users in the aspnet_Users table, just connect to your database using System.Data.SqlClient objects and query the table. There is no reason (you mentioned) that you need to use a membership provider to get this data.

Having said that, your membership / authentication scheme sounds like it might have some design problems, maybe it’s best to solve it in another question, but I think it might be useful for you if you ask for a comment that You are trying to perform generally with multiple membership providers.

Edit: I found some potentially useful posts about using multiple membership providers. It seems like the general idea is to inject custom code that handles the Login.Authenticate event in your Login control and use Membership.Providers["ProviderName"].ValidateUser to attempt authentication with each provider.

http://www.stevideter.com/2008/03/20/using-two-membership-providers-for-aspnet-logins/ http://forums.asp.net/p/1112089/1714276.aspx

0
source

Source: https://habr.com/ru/post/1369237/


All Articles