//Function - takes an accessToken and gets user info from facebook public async Task <FacebookUserAuth> GetFBUserInfo(string accessToken) { //Creates the correct Url to access the facebook api string url = "https://graph.facebook.com/me?access_token=" + accessToken + "&fields=email"; var httpClient = new HttpClient(); HttpResponseMessage httpResponseMessage; FacebookUserAuth content = new FacebookUserAuth(); try { //Calls the facebook api to get the user info httpResponseMessage = await httpClient.GetAsync(url); httpResponseMessage.EnsureSuccessStatusCode(); //Converts the http response to a JSON string string strCont = await httpResponseMessage.Content.ReadAsStringAsync(); //Converts the string into a C# object we can use JavaScriptSerializer js = new JavaScriptSerializer(); content = js.Deserialize <FacebookUserAuth>(strCont); } catch (Exception ex) { log.Error("Error getting FaceBook user information", ex); } return(content); }
public async Task <HttpResponseMessage> RegisterThirdParty(string provider, ThirdPartyCompleteModel model) { string errorMsg = "provider"; try { Provider type; //Checks the provider passed in through the api call and assigns the type and error message if linkedin, google, or facebook switch (provider) { case "linkedin": type = Provider.LinkedIn; errorMsg = "LinkedIn"; break; case "google": type = Provider.Google; errorMsg = "Google"; break; case "facebook": type = Provider.Facebook; errorMsg = "Facebook"; break; default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No provider exists with this name")); } //If Facebook login call the service to get information from facebook if (type == Provider.Facebook) { FacebookUserAuth userInfo = await _service.GetFBUserInfo(model.AccessToken); model.Email = userInfo.Email; model.ProviderId = userInfo.Id; } //Checks if the email exists in the database int accountId = _service.CheckEmail(model.Email); //If the email doesn't exist we need to register a new account if (accountId == 0) { //Pass the information and role into the register function, and get back the new account id accountId = Register(model, model.Role); //If something went wrong return an error if (accountId == -1) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Login Error")); } } SocialProviderUser spu = new SocialProviderUser(); spu.Email = model.Email; spu.Id = accountId; spu.ProviderId = model.ProviderId; //Send the email, account id, and provider id to the login function if (_service.LogIn(spu, type)) { //If the login was successful, get the user information and send it back ItemResponse <PersonViewModel> resp = new ItemResponse <PersonViewModel>(); resp.Item = _service.SelectPerson(spu.Id); return(Request.CreateResponse(HttpStatusCode.OK, resp)); } else { //If the login was unsuccessful, return an error return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong logging in with " + errorMsg)); } } catch (Exception ex) { //If an exception occurs log it and return an error log.Error("Error registering " + errorMsg + " account", ex); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong")); } }