public async Task <UserEnvelope> Handle(Command request, CancellationToken cancellationToken) { var person = await _context.Persons .Where(x => x.Username == request.User.Username) .AsNoTracking() .SingleOrDefaultAsync(cancellationToken); if (person == null) { throw new RestException(HttpStatusCode.Unauthorized, new { Error = "Invalid email / password." }); } if (!_passwordHasher.Verify(request.User.Password, person.Password)) { throw new RestException(HttpStatusCode.Unauthorized, new { Error = "Invalid email / password." }); } var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); user.IsCurrentUser = true; /* * Trace user login */ var userAgent = _currentUserAccessor.GetUserAgent(); var description = $"LoggedIn using ip: {_currentUserAccessor.GetUserIp()}, " + $"UserAgent: {userAgent.OS} {userAgent.Name} {userAgent.Version}"; var audit = new Audit() { Descriptions = description, CreatedDate = DateTime.Now, PersonId = person.Id }; await _context.Audits.AddAsync(audit, cancellationToken); await _context.SaveChangesAsync(cancellationToken); user.Token = _jwtTokenGenerator.ValidTokenTime(ValidPeriodUser).CreateToken(person.Username, user.Type, audit.Id.ToString()); return(new UserEnvelope(user)); }