连接字符串之间的C#交换web.config成员资格提供程序

时间:2017-12-04 14:56:34

标签: c# connection-string asp.net-membership membership-provider

在应用程序中,我们有2个成员资格提供者的连接字符串。 我们根据2个连接字符串及其属性创建用户:用户为minRequiredNonalphanumericCharacters="2" minRequiredPasswordLength="8" maxInvalidPasswordAttempts="5",管理员为minRequiredNonalphanumericCharacters="1" minRequiredPasswordLength="6" maxInvalidPasswordAttempts="5"。 我们如何在创建用户或管理员时更改活动连接字符串?

代码

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

1 个答案:

答案 0 :(得分:1)

也许你可以试试:

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