示例#1
0
        public async Task <ResultResponse> RegisterAsync(UserRegisterDto dto)
        {
            try
            {
                var user = (await _userRepository.GetAsync($"where lower(email) = lower('{dto.Email}')")).FirstOrDefault();
                if (user != null)
                {
                    return(ResultResponse.GetBadResponse(StatusCode.BadRequest, "Пользователь с таким Email уже зарегистрирован"));
                }

                var newUser = _mapper.Map <User>(dto);
                newUser.registered    = DateTime.Now;
                newUser.salt          = GenerateSalt();
                newUser.password_hash = await _passwordHashService.GetPasswordHashWithSalt(dto.Password, newUser.salt);

                var added = await _userRepository.AddAsync(newUser);

                if (added != null)
                {
                    return(ResultResponse.GetSuccessResponse());
                }
                else
                {
                    return(ResultResponse.GetInternalServerErrorResponse());
                }
            }
            catch (Exception ex)
            {
                _progressLogger.Error(ex, dto, GetType().Name, nameof(RegisterAsync));
                return(ResultResponse.GetInternalServerErrorResponse());
            }
        }
示例#2
0
        private async Task <ClaimsIdentity> GetIdentityAsync(string email, string password)
        {
            var user = (await _userRepository.GetAsync($"where lower(email) = lower('{email}')")).FirstOrDefault();

            if (user == null)             // Если пользователя не найдено
            {
                return(null);
            }
            var passwordHash = await _passwordHashService.GetPasswordHashWithSalt(password, user.salt);

            if (passwordHash == user.password_hash)             // Пароль проходит по хешу
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, user.id.ToString()),
                    new Claim(ClaimTypes.Email, user.email.ToString())
                };
                ClaimsIdentity claimsIdentity =
                    new ClaimsIdentity(claims, JwtBearerDefaults.AuthenticationScheme, ClaimsIdentity.DefaultNameClaimType,
                                       ClaimsIdentity.DefaultRoleClaimType);
                return(claimsIdentity);
            }

            // Если пароль не подошел
            return(null);
        }
示例#3
0
 public async Task GetHashWithSalt()
 {
     var hash = await hashService.GetPasswordHashWithSalt("Aa_02345678", "S31fF!sLxz)$#");
 }