public MembershipResult ChangePassword(ChangePasswordRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();
            var customer = CustomerProvider.QueryByUserName(request.Username);
            #region Validation
            if (customer == null)
            {
                result.Errors.Add("The customer does not exists.".Localize());
            }
            MembershipResult validateResult = null;
            if (string.IsNullOrEmpty(request.OldPassword) || !((validateResult = Validate(customer, request.OldPassword)).Success))
            {
                if (validateResult != null)
                {
                    result.Errors.AddRange(validateResult.Errors);
                }
            }
            if (string.IsNullOrEmpty(request.NewPassword))
            {
                result.Errors.Add("Invalid new password.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.NewPassword, customer.Membership.PasswordSalt);
                customer.Membership.Password = encryptedPwd;
                CustomerService.Update(customer);
            }
            return result;
        }
Beispiel #2
0
        public MembershipResult ResetPassowrd(ResetPasswordRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();
            var customer            = CustomerProvider.QueryByUserName(request.Username);

            #region Validation
            if (customer == null)
            {
                result.Errors.Add("The customer does not exists.".Localize());
            }
            if (request.ResetPasswordToken != customer.Membership.ResetPasswordToken)
            {
                result.Errors.Add("Invalid token.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.NewPassword, customer.Membership.PasswordSalt);
                customer.Membership.Password           = encryptedPwd;
                customer.Membership.ResetPasswordToken = null;
                CustomerService.Update(customer);
            }

            return(result);
        }
Beispiel #3
0
        public MembershipResult Register(RegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();

            #region Validation
            if (string.IsNullOrEmpty(request.Username))
            {
                result.Errors.Add("Invalid username".Localize());
            }
            if (string.IsNullOrEmpty(request.Email) || !Regex.IsMatch(request.Email, Kooboo.RegexPatterns.EmailAddress))
            {
                result.Errors.Add("Invalid email.".Localize());
            }
            if (CustomerProvider.QueryByUserName(request.Username) != null)
            {
                result.Errors.Add("The username already exists.".Localize());
            }
            if (CustomerProvider.QueryByEmail(request.Email) != null)
            {
                result.Errors.Add("The email already exists.".Localize());
            }
            if (string.IsNullOrEmpty(request.Password))
            {
                result.Errors.Add("Invalid password.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var passwordSalt = PasswordEncryptor.GenerateSalt();
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.Password, passwordSalt);
                var customer     = new Customer()
                {
                    Username   = request.Username,
                    Email      = request.Email,
                    Membership = new Membership()
                    {
                        Password            = encryptedPwd,
                        PasswordSalt        = passwordSalt,
                        IsApproved          = request.IsApproved,
                        UtcCreationDate     = DateTime.UtcNow,
                        UtcLastActivityDate = DateTime.UtcNow
                    }
                };
                CustomerService.Add(customer);
            }

            return(result);
        }
Beispiel #4
0
        private MembershipResult Validate(Customer customer, string password)
        {
            MembershipResult result = new MembershipResult();

            if (customer == null)
            {
                result.Errors.Add("Username and/or password are incorrect.".Localize());
            }

            else
            {
                if (customer.Membership.IsLockedOut || !customer.Membership.IsApproved)
                {
                    result.Errors.Add("Account was locked out or not approved.".Localize());
                }
                else
                {
                    var  encryptedPwd = PasswordEncryptor.EncryptPassword(password, customer.Membership.PasswordSalt);
                    bool isValid      = encryptedPwd == customer.Membership.Password;
                    if (isValid)
                    {
                        customer.Membership.UtcLastLoginDate           = DateTime.UtcNow;
                        customer.Membership.FailedPasswordAttemptCount = 0;
                        CustomerService.Update(customer);
                    }
                    else
                    {
                        customer.Membership.FailedPasswordAttemptCount = customer.Membership.FailedPasswordAttemptCount + 1;
                        if (customer.Membership.FailedPasswordAttemptCount >= 5)
                        {
                            customer.Membership.IsLockedOut = true;
                        }

                        result.Errors.Add("Username and/or password are incorrect.".Localize());
                    }
                }
            }
            return(result);
        }
Beispiel #5
0
        public MembershipResult ChangePassword(ChangePasswordRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();
            var customer            = CustomerProvider.QueryByUserName(request.Username);

            #region Validation
            if (customer == null)
            {
                result.Errors.Add("The customer does not exists.".Localize());
            }
            MembershipResult validateResult = null;
            if (string.IsNullOrEmpty(request.OldPassword) || !((validateResult = Validate(customer, request.OldPassword)).Success))
            {
                if (validateResult != null)
                {
                    result.Errors.AddRange(validateResult.Errors);
                }
            }
            if (string.IsNullOrEmpty(request.NewPassword))
            {
                result.Errors.Add("Invalid new password.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.NewPassword, customer.Membership.PasswordSalt);
                customer.Membership.Password = encryptedPwd;
                CustomerService.Update(customer);
            }
            return(result);
        }
        private MembershipResult Validate(Customer customer, string password)
        {
            MembershipResult result = new MembershipResult();
            if (customer == null)
            {
                result.Errors.Add("Username and/or password are incorrect.".Localize());
            }

            else
            {
                if (customer.Membership.IsLockedOut || !customer.Membership.IsApproved)
                {
                    result.Errors.Add("Account was locked out or not approved.".Localize());
                }
                else
                {
                    var encryptedPwd = PasswordEncryptor.EncryptPassword(password, customer.Membership.PasswordSalt);
                    bool isValid = encryptedPwd == customer.Membership.Password;
                    if (isValid)
                    {
                        customer.Membership.UtcLastLoginDate = DateTime.UtcNow;
                        customer.Membership.FailedPasswordAttemptCount = 0;
                        CustomerService.Update(customer);
                    }
                    else
                    {

                        customer.Membership.FailedPasswordAttemptCount = customer.Membership.FailedPasswordAttemptCount + 1;
                        if (customer.Membership.FailedPasswordAttemptCount >= 5)
                        {
                            customer.Membership.IsLockedOut = true;
                        }

                        result.Errors.Add("Username and/or password are incorrect.".Localize());
                    }
                }

            }
            return result;
        }
        public MembershipResult ResetPassowrd(ResetPasswordRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();
            var customer = CustomerProvider.QueryByUserName(request.Username);
            #region Validation
            if (customer == null)
            {
                result.Errors.Add("The customer does not exists.".Localize());
            }
            if (request.ResetPasswordToken != customer.Membership.ResetPasswordToken)
            {
                result.Errors.Add("Invalid token.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.NewPassword, customer.Membership.PasswordSalt);
                customer.Membership.Password = encryptedPwd;
                customer.Membership.ResetPasswordToken = null;
                CustomerService.Update(customer);
            }

            return result;
        }
        public MembershipResult Register(RegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            MembershipResult result = new MembershipResult();
            #region Validation
            if (string.IsNullOrEmpty(request.Username))
            {
                result.Errors.Add("Invalid username".Localize());
            }
            if (string.IsNullOrEmpty(request.Email) || !Regex.IsMatch(request.Email, Kooboo.RegexPatterns.EmailAddress))
            {
                result.Errors.Add("Invalid email.".Localize());
            }
            if (CustomerProvider.QueryByUserName(request.Username) != null)
            {
                result.Errors.Add("The username already exists.".Localize());
            }
            if (CustomerProvider.QueryByEmail(request.Email) != null)
            {
                result.Errors.Add("The email already exists.".Localize());
            }
            if (string.IsNullOrEmpty(request.Password))
            {
                result.Errors.Add("Invalid password.".Localize());
            }
            #endregion

            if (result.Success)
            {
                var passwordSalt = PasswordEncryptor.GenerateSalt();
                var encryptedPwd = PasswordEncryptor.EncryptPassword(request.Password, passwordSalt);
                var customer = new Customer()
                {
                    Username = request.Username,
                    Email = request.Email,
                    Membership = new Membership()
                    {
                        Password = encryptedPwd,
                        PasswordSalt = passwordSalt,
                        IsApproved = request.IsApproved,
                        UtcCreationDate = DateTime.UtcNow,
                        UtcLastActivityDate = DateTime.UtcNow
                    }
                };
                CustomerService.Add(customer);
            }

            return result;
        }