public UserViewModel Authenticate(string username, string password) { var response = ServerResponse.OK; var result = _userRepository.Get().SingleOrDefault(x => x.UserName == username); if (result == null) { return(_errorMapper.MapToError(null, ServerResponse.BadRequest, "User is not found.")); } bool isValid = _cryptographyHandler.VerifyGeneratedHash(password, result.Password); if (!isValid) { return(_errorMapper.MapToError(null, ServerResponse.BadRequest, "Username or password is incorrect.")); } var user = _mapper.Map <UserViewModel>(result); user.Token = _jwtTokenHandler.GenerateJwtSecurityToken(user.UserId.ToString()); user.Password = null; return(user); }