public IActionResult Facebook([FromBody] AccessTokenDTO model) { var client = _clientFactory.CreateClient(); // 1.generate an app access token var appAccessTokenResponse = client.GetAsync($"https://graph.facebook.com/oauth/access_token?client_id={_appSettings.FacebookAppId}&client_secret={_appSettings.FacebookAppSecret}&grant_type=client_credentials"); var responseString = appAccessTokenResponse.Result.Content.ReadAsStringAsync().Result; AccessTokenDTO appAccessToken = JsonConvert.DeserializeObject <AccessTokenDTO>(responseString); // 2. validate the user access token var userAccessTokenValidationResponse = client.GetAsync($"https://graph.facebook.com/debug_token?input_token={model.AccessToken}&access_token={appAccessToken.AccessToken}"); var userAccessResponseString = userAccessTokenValidationResponse.Result.Content.ReadAsStringAsync().Result; var userAccessTokenValidation = JsonConvert.DeserializeObject <FacebookUserAccessTokenValidation>(userAccessResponseString); if (!userAccessTokenValidation.Data.IsValid) { return(BadRequest(new { code = false, message = "Invalid facebook token." })); } // 3. we've got a valid token so we can request user data from fb var userInfoResponse = client.GetAsync($"https://graph.facebook.com/v2.8/me?fields=id,email,first_name,last_name,name,gender,locale,birthday,picture&access_token={model.AccessToken}"); var userInfoResponseString = userInfoResponse.Result.Content.ReadAsStringAsync().Result; var userInfo = JsonConvert.DeserializeObject <ExternalLoginDTO>(userInfoResponseString); // 4. ready to create the local user account (if necessary) and jwt var user = _userService.FindByEmail(userInfo.Email); if (user == null) { var appUser = new User { Email = userInfo.Email, }; _userService.CreateExternalUser(appUser); } // generate the jwt for the local user... var localUser = _userService.FindByEmail(userInfo.Email); if (localUser == null) { return(BadRequest(new { code = false, message = "Failed to create local user account." })); } _externalLoginService.CreateOrUpdate(localUser, userInfo, ExternalLogin.FACEBOOK); var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(_appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, localUser.Id.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); // return basic user info (without password) and token to store client side return(Ok(new { status = true, userData = new { Id = localUser.Id, Email = localUser.Email, Phone = localUser.Phone, ReferralCode = localUser.ReferralCode, Token = tokenString } })); }