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(); } }
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); } }
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)); } }
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); }
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"))); } }
private static LoginAttemptConfirmViewModel BuildLoginAttemptConfirmViewModel(Guid id, LoginAttemptDto loginAttempt) { return(new() { Id = id, ExpiredOrNonExisting = loginAttempt == null || loginAttempt.Status == LoginAttemptStatus.Expired, WasAlreadyConfirmed = loginAttempt?.Status == LoginAttemptStatus.Approved }); }