public CreateUserStatus CreateUser( RegistrationData user,
            [Required( ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof( ValidationErrorResources ) )]
            [RegularExpression( "^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof( ValidationErrorResources ) )]
            [StringLength( 50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof( ValidationErrorResources ) )]
            string password )
        {
            if ( user == null )
            {
                throw new ArgumentNullException( "user" );
            }

            // Run this BEFORE creating the user to make sure roles are enabled and the default role
            // will be available
            //
            // If there are a problem with the role manager it is better to fail now than to have it
            // happening after the user is created
            if ( !Roles.RoleExists( UserRegistrationService.DefaultRole ) )
            {
                Roles.CreateRole( UserRegistrationService.DefaultRole );
            }

            // NOTE: ASP.NET by default uses SQL Server Express to create the user database.
            // CreateUser will fail if you do not have SQL Server Express installed.
            MembershipCreateStatus createStatus;
            var membershipUser = Membership.CreateUser( user.UserName, password, user.Email, user.Question, user.Answer, true, null, out createStatus );

            if ( createStatus != MembershipCreateStatus.Success )
            {
                // return new UserContainer(UserRegistrationService.ConvertStatus(createStatus),membershipUser.ProviderUserKey);
                return UserRegistrationService.ConvertStatus( createStatus );
            }

            // Assign it to the default role
            // This *can* fail but only if role management is disabled
            Roles.AddUserToRole( user.UserName, DefaultRole );

            // Set its friendly name (profile setting)
            // This *can* fail but only if Web.config is configured incorrectly
            ProfileBase profile = ProfileBase.Create( user.UserName, true );
            profile.SetPropertyValue( "FriendlyName", user.FriendlyName );
            profile.Save();

            //Insert member
            GetUserIdAndInsertMember( user.UserName, new Guid( membershipUser.ProviderUserKey.ToString() ) );

            //return new UserContainer(CreateUserStatus.Success, membershipUser.ProviderUserKey);

            return CreateUserStatus.Success;
        }
示例#2
0
        void OnSaveChanges()
        {
            var data = new RegistrationData();

            try
            {
                data.Answer = UserToInsert.Answer;
                data.Email = UserToInsert.Email;
                data.FriendlyName = UserToInsert.FriendlyName;
                data.Password = UserToInsert.Password;
                data.PasswordConfirmation = UserToInsert.PasswordConfirmation;
                data.Question = UserToInsert.Question;
                data.UserName = UserToInsert.UserName;

                Validator.ValidateProperty(UserToInsert.UserName,
                   new ValidationContext(data, null, null) { MemberName = "UserName" });

                Validator.ValidateProperty(UserToInsert.Email,
                 new ValidationContext(data, null, null) { MemberName = "Email" });

                Validator.ValidateProperty(UserToInsert.Password,
                new ValidationContext(data, null, null) { MemberName = "Password" });

                Validator.ValidateProperty(UserToInsert.PasswordConfirmation,
                new ValidationContext(data, null, null) { MemberName = "PasswordConfirmation" });

                Validator.ValidateProperty(UserToInsert.Question,
               new ValidationContext(data, null, null) { MemberName = "Question" });

                Validator.ValidateProperty(UserToInsert.Answer,
             new ValidationContext(data, null, null) { MemberName = "Answer" });

                if (UserToInsert.Password != UserToInsert.PasswordConfirmation)
                {
                    throw new ValidationException("Password and confirmation are not the same");
                }
            }
            catch (ValidationException ex)
            {
                var member = ((string[])(ex.ValidationResult.MemberNames)).Count() > 0
                                  ? ((string[]) (ex.ValidationResult.MemberNames))[0]
                                  : string.Empty;

                AppMessages.MemberValidationMessage.Send(member + " - " + ex.Message);
                return;
            }

            _userContext.CreateUser(data, UserToInsert.Password, CreationOfMemberCompleted, null);
        }