public User CreateUser(string username, string email, string password, int[] roles)
        {
            var existingUser = _userRepository.GetSingleByUsername(username);

            if (existingUser != null)
            {
                throw new Exception(String.Format("Username {0} is already in use", username));
            }

            var passwordSalt = _encryptionService.CreateSalt();

            var user = new User()
            {
                Username = username,
                Salt = passwordSalt,
                Email = email,
                IsLocked = false,
                HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt),
                DateCreated = DateTime.Now
            };

            _userRepository.Add(user);

            _unitOfWork.Commit();

            if (roles != null || roles.Length > 0)
            {
                foreach (var role in roles)
                {
                    assUserToRole(user, role);
                }
            }

            _unitOfWork.Commit();

            return user;
        }
 private bool isPasswordValid(User user, string password)
 {
     return string.Equals(_encryptionService.EncryptPassword(password, user.Salt), user.HashedPassword);
 }
 private bool isUserValid(User user, string password)
 {
     if (isPasswordValid(user, password))
     {
         return !user.IsLocked;
     }
     return false;
 }
        private void assUserToRole(User user, int roleId)
        {
            var role = _roleRepository.GetSingle(roleId);
            if (role == null)
                throw new ApplicationException("Role doesn't exist");

            var userRole = new UserRole()
            {
                RoleId = role.ID,
                UserId = user.ID
            };
            _userRoleRepository.Add(userRole);
        }