示例#1
0
        public void CreateNew(Account account)
        {
            if (UserExists(account.Username))
            {
                throw new UserAlreadyExistsException();
            }

            if (InvalidUserName(account.Username))
            {
                throw new InvalidUsernameException();
            }

            if (InvalidEmailAddres(account.EmailAddress))
            {
                throw new InvalidEmailAddressException();
            }

            var user = new WebSecurityUser();

            user.SetDefaultStatistics("system");

            // Copy over default stats
            account.LastModified        = user.Statistics.LastModified.Value;
            account.LastLogin           = user.Statistics.LastLogin.Value;
            account.LastLoginAttempted  = user.Statistics.LastLoginAttempted.Value;
            account.LastPasswordChanged = user.Statistics.LastPasswordChanged.Value;

            account.Password =
                _passwordManager.EncryptPassword(account.Password, BCryptEncoder.GenerateSalt(), BCryptEncoder.HashPassword);

            _repository.Save(account);
        }
        public void CreateUser(WebSecurityUser user)
        {
            var assertions = new List <PolicyAssertion>
            {
                new PasswordLengthPolicy(),
                new PasswordStrengthPolicy(),
                new PasswordCharactersPolicy()
            };

            _policyEnforcer.EnforceUniqueUser(user);
            _policyEnforcer.EnforcePasswordPolicies(user, assertions);

            user.Username = user.NewUsername;
            user.Password = _passwordManager.EncryptPassword(user.NewPassword, BCryptEncoder.GenerateSalt(), BCryptEncoder.HashPassword);

            user.SetDefaultStatistics(Authorizer.Username);

            _dataProvider.CreateUser(user);
        }