Search code examples
c#connection-stringasp.net-membershipmembership-provider

C# swap between connection strings web.config Membership provider


In the application we have 2 connection strings for membership provider. We create users based on the 2 connection strings and their properties: minRequiredNonalphanumericCharacters="2" minRequiredPasswordLength="8" maxInvalidPasswordAttempts="5" for users and minRequiredNonalphanumericCharacters="1" minRequiredPasswordLength="6" maxInvalidPasswordAttempts="5" for admins. How we can change the active connection string when creating a user or an admin?

Code

//Code needed to swap to User connection string
MembershipUser newUser = Membership.CreateUser(username, password, email, question, answer, isAproved, out cs);
Membership.UpdateUser(newUser);
Roles.AddUserToRole(username, "User");

//Code needed to swap to Admin connection string
MembershipUser newUserAdmin = Membership.CreateUser(usernameAdmin, passwordAdmin, emailAdmin, questionAdmin, answerAdmin, isAprovedAdmin, out cs);
Membership.UpdateUser(newUserAdmin);
Roles.AddUserToRole(usernameAdmin, "Administrator");

Solution

  • Maybe you can try that:

    var p = (SqlMembershipProvider)Membership.Providers["name_of_membership_provider"];
    
    MembershipUser newUser = p.CreateUser(username, password, email, question, answer, isAproved, null, out cs);
    p.UpdateUser(newUser);