Exemple #1
0
        /// <summary>
        /// Creates a new user.
        /// </summary>
        /// <param name="id">An ID for the user to create.</param>
        /// <param name="name">A name for the user to create.</param>
        /// <param name="password">A password for the user.</param>
        /// <param name="role">A user role to assign to the user.</param>
        /// <returns>Returns the newly created user.</returns>
        /// <exception cref="UserAlreadyExistsException">Thrown if the login name is already taken.</exception>
        public User CreateUser(string id, string name, string password, Role role)
        {
            if (UserRepository.GetUser(id) != null)
            {
                throw new UserAlreadyExistsException(id);
            }

            // Hash & salt password, create user!
            (string hash, byte[] salt) = PasswordHashingService.HashAndSaltPassword(password);
            return(UserRepository.CreateUser(id, name, hash, salt, role));
        }
Exemple #2
0
        /// <summary>
        /// Attempts to change a user's password
        /// </summary>
        /// <param name="id">The ID of the user.</param>
        /// <param name="oldPassword">The old password for verification.</param>
        /// <param name="newPassword">The new password to save.</param>
        /// <exception cref="UserNotFoundException">Thrown if there is no such user.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown if the submitted old password is wrong!</exception>
        public void ChangePassword(string id, string oldPassword, string newPassword)
        {
            User user = GetUserOrThrowNotFoundException(id);

            // Verify old password
            if (user.Password != PasswordHashingService.HashAndSaltPassword(oldPassword, user.Salt))
            {
                throw new UnauthorizedAccessException();
            }

            // Hash and salt new password
            (string hashedPassword, byte[] salt) = PasswordHashingService.HashAndSaltPassword(newPassword);
            user.Password = hashedPassword;
            user.Salt     = salt;
            UserRepository.UpdateUser(user);
        }
Exemple #3
0
 /// <summary>
 /// Sets up the service.
 /// </summary>
 /// <param name="loggerFactory">Fasctory to create loggers from.</param>
 /// <param name="passwordHashingService">Provides hashing features.</param>
 /// <param name="userRepository">Repository for user data.</param>
 public UserService(ILoggerFactory loggerFactory, PasswordHashingService passwordHashingService, IUserRepository userRepository)
 {
     Logger = loggerFactory.CreateLogger <UserService>();
     PasswordHashingService = passwordHashingService;
     UserRepository         = userRepository;
 }