コード例 #1
0
        private async Task <bool> ChangePasswordToAsync(UserEntity user, string oldPassword, string newPassword)
        {
            if (user is null)
            {
                _logger.LogError($"{nameof(UserSettingsService)}:{nameof(ChangePasswordToAsync)}:{nameof(user)} was null");

                throw new ArgumentNullException(nameof(user));
            }

            if (newPassword is null)
            {
                _logger.LogError($"{nameof(UserSettingsService)}:{nameof(ChangePasswordToAsync)}:{nameof(newPassword)} was null");

                throw new ArgumentNullException(nameof(newPassword));
            }

            if (oldPassword is null)
            {
                _logger.LogError($"{nameof(UserSettingsService)}:{nameof(ChangePasswordToAsync)}:{nameof(oldPassword)} was null");

                throw new ArgumentNullException(nameof(oldPassword));
            }

            var oldHashedPassword = _passwordEncryptorService.EncryptPassword(oldPassword);

            if (oldHashedPassword.Equals(user.HashedPassword, StringComparison.Ordinal))
            {
                return(await ChangePasswordToAsync(user, newPassword));
            }

            _logger.LogWarning($"Old password in hash is not equal to database's hashed password for user with id = {user.Id}");

            return(false);
        }
コード例 #2
0
        private async Task <bool> RegisterAsync(string email, string rawPassword, RoleEntity role)
        {
            if (email is null)
            {
                _logger.LogError($"{nameof(RegistrationService)}: email was null when tried to register user");

                throw new ArgumentNullException(nameof(email));
            }

            if (rawPassword is null)
            {
                _logger.LogError($"{nameof(RegistrationService)}: raw password was null when tried to register user");

                throw new ArgumentNullException(nameof(rawPassword));
            }

            if (role is null)
            {
                _logger.LogError($"{nameof(RegistrationService)}: role was null when tried to register user");

                throw new ArgumentNullException(nameof(role));
            }

            var userAlreadyExists = await _unitOfWorkUsersIdentity.UsersRepository
                                    .AnyAsync(user => user.Email == email);

            if (userAlreadyExists)
            {
                _logger.LogWarning($"{nameof(RegistrationService)}: user with email = {email} is already exist");

                return(false);
            }

            var hashedPassword = _encryptorService.EncryptPassword(rawPassword);

            var newUser = new UserEntity
            {
                Email            = email,
                HashedPassword   = hashedPassword,
                IsActive         = true,
                IsBlocked        = false,
                RegistrationDate = DateTime.Now,
                RoleId           = role.Id,
                Role             = role
            };

            await _unitOfWorkUsersIdentity.UsersRepository.AddAsync(newUser);

            var saveChangesResult = await _unitOfWorkUsersIdentity.SaveChangesAsync();

            await _emailService.SendRegistrationNotificationAsync(email);

            return(saveChangesResult > 0);
        }
コード例 #3
0
        /// <exception cref="T:System.ArgumentNullException"><paramref name="emailOrLogin"/> is <see langword="null"/></exception>
        /// <exception cref="T:System.Reflection.TargetInvocationException">
        ///     The algorithm was used with Federal Information
        ///     Processing Standards (FIPS) mode enabled, but is not FIPS compatible.
        /// </exception>
        /// <exception cref="T:System.ObjectDisposedException">The object has already been disposed.</exception>
        /// <exception cref="T:System.Text.EncoderFallbackException">
        ///     A fallback occurred (for more information, see Character Encoding in .NET)
        ///     -and-
        ///     <see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>
        ///     .
        /// </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">capacity is less than zero.</exception>
        /// <exception cref="T:System.FormatException">
        ///     format includes an unsupported specifier. Supported
        ///     format specifiers are listed in the Remarks section.
        /// </exception>
        /// <exception cref="T:System.ArgumentException">Hashed password should be 128 length, but it hasn't.</exception>

        #endregion

        public Task <UserEntityReadDto> FindUserByEmailOrLoginAndPasswordAsync(string emailOrLogin,
                                                                               string password)
        {
            if (emailOrLogin is null)
            {
                _logger.LogError($"{nameof(UsersInformationService)}: Find User By Email Or SignIn And Password: email or login was null");

                throw new ArgumentNullException(nameof(emailOrLogin));
            }

            if (password is null)
            {
                _logger.LogError($"{nameof(UsersInformationService)}: Find User By Email Or SignIn And Password: password was null");

                throw new ArgumentNullException(nameof(password));
            }

            var hashedPassword = _passwordEncryptorService.EncryptPassword(password);

            return(FindUserByEmailOrLoginAndHashedPasswordAsync(emailOrLogin, hashedPassword));
        }