コード例 #1
0
        public void Test_ChangePassword()
        {
            var customer = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                Membership = new Membership()
                {
                    Password = "******",
                    IsApproved = true
                }
            };
            var customerProvider = new Mock<ICustomerProvider>();
            customerProvider.Setup(it => it.QueryByUserName("test"))
                .Returns(customer);

            var customerService = new Mock<ICustomerService>();
            Customer updateCustomer = null;
            customerService.Setup(it => it.Update(It.IsAny<Customer>()))
                .Callback<Customer>((c) =>
                {
                    updateCustomer = c;
                });

            var passwordEncryptor = new Mock<IPasswordEncryptor>();
            passwordEncryptor.Setup(it => it.EncryptPassword(It.IsAny<string>(), It.IsAny<string>()))
                .Returns<string, string>((p, salt) => p);

            CustomerMembershipService customerMemberhipService = new CustomerMembershipService(
              customerProvider.Object, customerService.Object, passwordEncryptor.Object);

            ChangePasswordRequest request = new ChangePasswordRequest()
            {
                Username = "******",
                NewPassword = "******",
                OldPassword = "******"
            };
            var result = customerMemberhipService.ChangePassword(request);
            Assert.IsTrue(result.Success);
            Assert.IsNotNull(updateCustomer);
            Assert.AreEqual(request.Username, updateCustomer.Username);
            Assert.AreEqual(request.NewPassword, updateCustomer.Membership.Password);
        }
コード例 #2
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;
        }
コード例 #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;
        }
コード例 #4
0
        public void Test_ValidateByUserName_Return_True()
        {
            var customer = new Customer()
                {
                    Username = "******",
                    Membership = new Membership()
                    {
                        IsLockedOut = false,
                        IsApproved = true,
                        Password = "******"
                    }
                };
            var customerProvider = new Mock<ICustomerProvider>();
            customerProvider.Setup(it => it.QueryByUserName("test"))
                .Returns(customer);
            var customerService = new Mock<ICustomerService>();
            var passwordEncryptor = new Mock<IPasswordEncryptor>();
            passwordEncryptor.Setup(it => it.EncryptPassword("test", null))
                .Returns("test");

            CustomerMembershipService customerMemberhipService = new CustomerMembershipService(
                customerProvider.Object, customerService.Object, passwordEncryptor.Object);

            Assert.IsTrue(customerMemberhipService.ValidateByUserName("test", "test").Success);

            Assert.AreEqual(0, customer.Membership.FailedPasswordAttemptCount);
        }
コード例 #5
0
        public void Test_ValidateByUserName_PasswordInvalid_WithLockedOut()
        {
            var customer = new Customer()
               {
                   Username = "******",
                   Membership = new Membership()
                   {
                       IsLockedOut = false,
                       IsApproved = true,
                       Password = "******",
                       FailedPasswordAttemptCount = 4
                   }
               };
            var customerProvider = new Mock<ICustomerProvider>();
            customerProvider.Setup(it => it.QueryByUserName("test"))
               .Returns(customer);
            var customerService = new Mock<ICustomerService>();
            var passwordEncryptor = new Mock<IPasswordEncryptor>();

            CustomerMembershipService customerMemberhipService = new CustomerMembershipService(
                customerProvider.Object, customerService.Object, passwordEncryptor.Object);

            var result = customerMemberhipService.ValidateByUserName("test", "test");
            Assert.IsFalse(result.Success);
            Assert.AreEqual("Username and/or password are incorrect.", result.Errors[0]);

            Assert.AreEqual(5, customer.Membership.FailedPasswordAttemptCount);
            Assert.IsTrue(customer.Membership.IsLockedOut);
        }
コード例 #6
0
        public void Test_ResetPassowrdResult_Invalid_Token()
        {
            var customer = new Customer()
            {
                Username = "******",
                Email = "*****@*****.**",
                Membership = new Membership()
                {
                    Password = "******",
                    IsApproved = true,
                    ResetPasswordToken = "123456"
                }
            };
            var customerProvider = new Mock<ICustomerProvider>();
            customerProvider.Setup(it => it.QueryByUserName("test"))
                .Returns(customer);

            var customerService = new Mock<ICustomerService>();
            Customer updateCustomer = null;
            customerService.Setup(it => it.Update(It.IsAny<Customer>()))
                .Callback<Customer>((c) =>
                {
                    updateCustomer = c;
                });

            var passwordEncryptor = new Mock<IPasswordEncryptor>();
            passwordEncryptor.Setup(it => it.EncryptPassword(It.IsAny<string>(), It.IsAny<string>()))
                .Returns<string, string>((p, salt) => p);

            CustomerMembershipService customerMemberhipService = new CustomerMembershipService(
              customerProvider.Object, customerService.Object, passwordEncryptor.Object);

            ResetPasswordRequest request = new ResetPasswordRequest()
            {
                Username = "******",
                NewPassword = "******",
                ResetPasswordToken = "abc123"
            };

            var result = customerMemberhipService.ResetPassowrd(request);
            Assert.IsFalse(result.Success);
            Assert.AreEqual("Invalid token.", result.Errors[0]);
            Assert.IsNull(updateCustomer);
        }