private void GenerateUserRecordsTask( long quantity, RepositoryOperationResult state )
        {
            state.StartDateTime = DateTime.Now;
            state.MaxIteration = quantity;
            state.State = RepositoryOperationResultState.InProcess;

            var users = _dataBase.GetCollection( UserCollectionName );

            var lastNumber = FindLastNumber( users, NameFieldName, _userRegex ) + 1;

            var random = new Random();
            for (int i = 0; i < quantity; i++, lastNumber++)
            {
                var userModel = new RegisterModel
                                    {
                                        UserName = string.Format( DefaultUserNameFormat, lastNumber ),
                                        Age = random.Next( 91 ) + 10,
                                        Gender = (Gender)(random.Next( 2 ) + 1),
                                        Password = DefaultUserPassword,
                                    };

                userModel.Email = userModel.UserName + "@maildomain.com";

                var userMembership = Membership.GetUser( userModel.UserName );
                if (userMembership != null) Membership.DeleteUser( userModel.UserName );

                RegisterUser( userModel, false );

                state.IterationDone++;
            }

            state.EndDateTime = DateTime.Now;
            state.State = RepositoryOperationResultState.Success;
        }
        /// <summary>
        /// Registers the user.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="logon">if set to <c>true</c> user was logged on after successful registration.</param>
        /// <returns>Creation status</returns>
        public MembershipCreateStatus RegisterUser( RegisterModel model, bool logon )
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser( model.UserName, model.Password, model.Email, null, null, true, null, out createStatus );

            if (createStatus == MembershipCreateStatus.Success)
            {
                AddUserToRepository( model );

                if (logon)
                {
                    FormsAuthentication.SetAuthCookie( model.UserName, false /* createPersistentCookie */ );
                }
            }

            return createStatus;
        }
        private void AddUserToRepository( RegisterModel model )
        {
            var users = _dataBase.GetCollection<User>( UserCollectionName );

            var user = users.AsQueryable().FirstOrDefault( u => u.Name == model.UserName ) ??
                       new User
                           {
                               Id = ObjectId.GenerateNewId()
                           };
            //var user = users.FindOne( Query.EQ( "Name", model.UserName ) ) ?? new User
            //                                                                  {
            //                                                                      Id = ObjectId.GenerateNewId(),
            //                                                                  };

            user.Name = model.UserName;
            user.Email = model.Email;
            user.Age = model.Age;
            user.Gender = model.Gender;
            users.Save( user );

            var profile = ProfileBase.Create( user.Name );
            profile[UserIdFieldName] = user.Id.ToString();
            profile.Save();
        }
        public ActionResult Register( RegisterModel model )
        {
            if (ModelState.IsValid)
            {
                var repo = RepositoryFactory.GetRepository();
                var result = repo.RegisterUser( model, true );
                if (result == MembershipCreateStatus.Success)
                {
                    return RedirectToAction( "Index", "Home" );
                }
                else
                {
                    ModelState.AddModelError( "", ErrorCodeToString( result ) );
                }
            }

            // If we got this far, something failed, redisplay form
            return View( model );
        }