Esempio n. 1
0
        public async Task <User> LoginPasswordAsync(string email, string password)
        {
            var user = await _repository.ReadByEmailAsync(email)
                       ?? throw new EmailNotFoundException();

            if (!_hashGenerator.Verify(password, user.Salt, user.Password))
            {
                throw new InvalidPasswordException();
            }

            user.Token = _tokenGenerator.GenerateJwt(user.Id, user.Email, user.Username);

            return(user.WithoutSensitiveData());
        }
        public async Task <Account> Login(LoginModel loginModel)
        {
            var account = await _repository.Get(loginModel.Email);

            if (account == null)
            {
                throw new EmailNotFoundException();
            }

            if (!await _hasher.VerifyHash(loginModel.Password, account.Salt, account.Password))
            {
                throw new IncorrectPasswordException();
            }

            account.Token = _tokenGenerator.GenerateJwt(account.Id);

            return(account.WithoutSensitiveData());
        }
Esempio n. 3
0
        public async Task <BaseResponseDto <SignInDto> > Handle(SignInRequest request, CancellationToken cancellationToken)
        {
            var result = new BaseResponseDto <SignInDto>();

            try
            {
                var user = await _userManager.FindByEmailAsync(request.Email);

                if (user is null)
                {
                    throw new Exception("User does not exist");
                }
                var userSignInResult = await _userManager.CheckPasswordAsync(user, request.Password);

                if (userSignInResult)
                {
                    await _userManager.ResetAccessFailedCountAsync(user);

                    var roles = await _userManager.GetRolesAsync(user);

                    result.Data.Jwt = _tokenGenerator.GenerateJwt(user, roles);
                }
                else
                {
                    await _userManager.AccessFailedAsync(user);

                    int failcount = await _userManager.GetAccessFailedCountAsync(user);

                    if (failcount == 3)
                    {
                        await _userManager.SetLockoutEndDateAsync(user, new DateTimeOffset(DateTime.Now.AddMinutes(2)));
                    }
                }
            }
            catch (Exception e)
            {
                result.Errors.Add(e.Message);
            }
            return(result);
        }