atroul
atroul

Reputation: 219

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");

Upvotes: 1

Views: 91

Answers (1)

aggicd
aggicd

Reputation: 737

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);

Upvotes: 1

Related Questions