public bool UpdateUser(User userToUpdate, string oldPassword)
        {
            string newPassword = userToUpdate.Password ?? "";

            if (newPassword.Length < this.MinPasswordLength)
            {
                _userService.AddError("User.Password", "The password must be at least " + MinPasswordLength + " characters long.");
                return false;
            } 

            // The underlying ChangePassword() will throw an exception rather
            // than return false in certain failure scenarios.
            try
            {
                int nb;
                MembershipUserCollection v = _provider.GetAllUsers(0,10,out nb);
                MembershipUser currentUser = _provider.GetUser(userToUpdate.UserName, true /* userIsOnline */);

                //we update the password of the user in the aspnet membership management database
                if (currentUser.ChangePassword(oldPassword, userToUpdate.Password))
                    //we also update the password of the user in our application database
                    return _userService.EditUser(userToUpdate);

                return false;
            }
            catch (ArgumentException)
            {
                return false;
            }
            catch (MembershipPasswordException)
            {
                return false;
            }

        }
        public MembershipCreateStatus CreateUser(User userToCreate)
        {
            //convert null values to empty strings
            string userName = userToCreate.UserName ?? "";
            string password = userToCreate.Password ?? "";
            string email = userToCreate.Email ?? "";

            MembershipCreateStatus status;
            //we try to create the user in the aspnet membership management database
            _provider.CreateUser(userName, password,email, null, null, true, null, out status);

            //we create our "representation" of the user in our application database
            if (status == MembershipCreateStatus.Success)
            {
                if (!_userService.CreateUser(userToCreate))
                {//if failed, we delete the user previously create in the membership management database
                    _provider.DeleteUser(userName, true);
                    return MembershipCreateStatus.ProviderError;
                }
            }
            else
            {
                _userService.AddError("otherError", MembershipService.ErrorCodeToString(status));
            }
            return status;

        }
        public bool ValidateUser(User userToValidate)
        {
            //convert null values to empty strings
            userToValidate.LastName = userToValidate.LastName ?? "";
            userToValidate.FirstName = userToValidate.FirstName ?? "";
            userToValidate.UserName = userToValidate.UserName ?? "";
            userToValidate.Email = userToValidate.Email ?? "";
            userToValidate.Password = userToValidate.Password ?? "";
            userToValidate.ConfirmPassword = userToValidate.ConfirmPassword ?? "";

            if (userToValidate.FirstName.Trim().Length == 0)
                _validationDictionary.AddError("User.FirstName", "First Name is required.");
            if (userToValidate.LastName.Trim().Length == 0)
                _validationDictionary.AddError("User.LastName", "Last Name is required.");
            if (userToValidate.UserName.Trim().Length == 0)
                _validationDictionary.AddError("User.UserName", "User Name is required.");
            if (userToValidate.Password.Trim().Length == 0)
                _validationDictionary.AddError("User.Password", "Password is required.");
            else if(!userToValidate.Password.Equals(userToValidate.ConfirmPassword))
                _validationDictionary.AddError("User.ConfirmPassword", "The password and confirmation password do not match.");

            if (userToValidate.Email.Length == 0 )
                _validationDictionary.AddError("User.Email", "Email Address is required.");
            else if (!Regex.IsMatch(userToValidate.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
                _validationDictionary.AddError("User.Email", "Invalid Email Address.");
            
            return _validationDictionary.IsValid;
        }
        public void Delete(User userToDelete)
        {
            var originalUser = Get(userToDelete.Id);
            _entities.DeleteObject(originalUser);
            _entities.SaveChanges();

        }
        public User Create(User userToCreate)
        {
            _entities.AddToUserSet(userToCreate);
            _entities.SaveChanges();
            return userToCreate;

        }
        public User Update(User userToUpdate)
        {
            var originalUser = Get(userToUpdate.Id);
            _entities.ApplyCurrentValues(originalUser.EntityKey.EntitySetName, userToUpdate);
            _entities.SaveChanges();
            return userToUpdate;

        }
        public bool DeleteUser(User userToDelete)
        {
            string userName = userToDelete.UserName ?? "";

            //we delete the user in the aspnet membership management database
            if (_provider.DeleteUser(userToDelete.UserName, false))
            {  //we delete our "representation" of the user in our application database
                return _userService.DeleteUser(userToDelete);
            }
            return false;
        }
        public bool EditUser(User userToEdit)
        {
            // Validation logic
            if (!ValidateUser(userToEdit))
                return false;

            // Database logic
            try
            {
                _repository.Update(userToEdit);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool CreateUser(User userToCreate)
        {
            // Validation logic
            if (!ValidateUser(userToCreate))
                return false;

            // Database logic
            try
            {
                _repository.Create(userToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public CreateEditUserForm(User user, bool editMode)
        {
            InitializeComponent();

            _user = user;
            _editMode = editMode;
            _oldPassword = user.Password;

            _modelState = new Dictionary<string, string>();
            _membershipService = new MembershipService(new UserService(new SimpleModelStateWrapper(_modelState)));

            if (_editMode)
            {
                this.Text = "Edit User";
                this.createEditButton.Text = "Save";
                this.userNameTextBox.Enabled = false;
            }

            this.userBindingSource.DataSource = _user;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the UserSet EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToUserSet(User user)
 {
     base.AddObject("UserSet", user);
 }
 /// <summary>
 /// Create a new User object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="lastName">Initial value of the LastName property.</param>
 /// <param name="firstName">Initial value of the FirstName property.</param>
 /// <param name="userName">Initial value of the UserName property.</param>
 /// <param name="password">Initial value of the Password property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 /// <param name="isAdmin">Initial value of the IsAdmin property.</param>
 public static User CreateUser(global::System.Int32 id, global::System.String lastName, global::System.String firstName, global::System.String userName, global::System.String password, global::System.String email, global::System.Boolean isAdmin)
 {
     User user = new User();
     user.Id = id;
     user.LastName = lastName;
     user.FirstName = firstName;
     user.UserName = userName;
     user.Password = password;
     user.Email = email;
     user.IsAdmin = isAdmin;
     return user;
 }
 public bool DeleteUser(User userToDelete)
 {
     try
     {
         _repository.Delete(userToDelete);
     }
     catch
     {
         return false;
     }
     return true;
 }