コード例 #1
0
        private void CreateUserAccount()
        {
            // Create new user and retrieve create status result.
            MembershipCreateStatus status;
            string passwordQuestion = "Question";
            string passwordAnswer   = "Answer";

            try
            {
                MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text,
                                                               EmailTextbox.Text, passwordQuestion,
                                                               passwordAnswer, true, out status);
                if (newUser == null)
                {
                    DisplayMessage(false, MembershipHelper.GetErrorMessage(status));
                }
                else
                {
                    DisplayMessage(true, "User created successfully");

                    // Assign the role to the user
                    if (!string.IsNullOrEmpty(DropDownListRoles.SelectedValue))
                    {
                        Roles.AddUserToRole(newUser.UserName, DropDownListRoles.SelectedValue);
                    }

                    // Update profile
                    ProfileCommon currentProfile = new ProfileCommon();
                    currentProfile = Profile.GetProfile(newUser.UserName);

                    currentProfile.FirstName = FirstNameTextbox.Text;
                    currentProfile.LastName  = LastNameTextBox.Text;
                    currentProfile.Phone     = ContactTelNoTextBox.Text;
                    currentProfile.Save();
                }
            }
            catch
            {
                DisplayMessage(false, "An exception occurred creating the user.");
            }
        }