Ejemplo n.º 1
0
        public async Task LogLoginAttempt(string username, string userAgentString, string publicIp, LoginStatus status)
        {
            var clientInfo      = Parser.GetDefault().Parse(userAgentString);
            var loginAttemptDto = new LoginAttemptDto
            {
                Username    = username,
                Device      = clientInfo.Device.Family,
                Os          = clientInfo.OS.Family,
                Browser     = clientInfo.UA.Family,
                PublicIp    = publicIp,
                DateAndTime = DateTime.Now,
                Status      = status
            };

            try
            {
                loginAttemptDto.Location = _mapper.Map <LoginLocationDto>(await _ipStackClient.GetLocation(publicIp));

                await _context.LoginAttempt.AddAsync(_mapper.Map <LoginAttemptEntity>(loginAttemptDto));

                await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                await _context.LoginAttempt.AddAsync(_mapper.Map <LoginAttemptEntity>(loginAttemptDto));

                await _context.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
 private async Task DeleteLoginAttemptIfExpiredOrApproved(LoginAttemptDto loginAttemptDto,
                                                          LoginAttempt loginAttempt, CancellationToken cancellationToken)
 {
     if (loginAttemptDto.Status == LoginAttemptStatus.Expired ||
         loginAttemptDto.Status == LoginAttemptStatus.Approved)
     {
         _loginAttemptRepository.Delete(loginAttempt);
         await _unitOfWork.CommitAsync(cancellationToken);
     }
 }
Ejemplo n.º 3
0
        private async Task LoginIfApproved(LoginAttemptDto loginAttemptDto, bool rememberLogin, string clientId)
        {
            if (loginAttemptDto.Status == LoginAttemptStatus.Approved)
            {
                var user = await _userManager.FindByIdAsync(loginAttemptDto.UserId.ToString());

                await _signInManager.SignInAsync(user, rememberLogin, "email");

                await _events.RaiseAsync(new UserLoginSuccessEvent(user.Email, user.Id, user.UserName,
                                                                   clientId : clientId));
            }
        }
Ejemplo n.º 4
0
        public async Task <LoginAttemptStatus> Handle(DoLoginCommand request, CancellationToken cancellationToken)
        {
            var loginAttempt = await _loginAttemptRepository.GetByIdAsync(request.LoginAttemptId, cancellationToken);

            if (loginAttempt == null)
            {
                return(LoginAttemptStatus.Deleted);
            }

            var loginAttemptDto = LoginAttemptDto.FromLoginAttempt(loginAttempt);

            await LoginIfApproved(loginAttemptDto, request.RememberLogin, request.ClientId);

            await DeleteLoginAttemptIfExpiredOrApproved(loginAttemptDto, loginAttempt, cancellationToken);

            return(loginAttemptDto.Status);
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <LoginResultDto> > Login([FromBody] LoginAttemptDto loginInfo)
        {
            try
            {
                var result = await _accountService.LoginAsync(loginInfo);

                if (result.ResultType == LoginResultTypeEnum.Success)
                {
                    return(Ok(result));
                }
                else
                {
                    return(BadRequest(new ErrorResult(result.ResultType.GetDescription())));
                }
            }
            catch
            {
                return(BadRequest(new ErrorResult("Oops, something went wrong! Please try again")));
            }
        }
Ejemplo n.º 6
0
 private static LoginAttemptConfirmViewModel BuildLoginAttemptConfirmViewModel(Guid id, LoginAttemptDto loginAttempt)
 {
     return(new()
     {
         Id = id,
         ExpiredOrNonExisting = loginAttempt == null || loginAttempt.Status == LoginAttemptStatus.Expired,
         WasAlreadyConfirmed = loginAttempt?.Status == LoginAttemptStatus.Approved
     });
 }