private void PopulateUserDataUsingFacebook() { var oAuth = new oAuthFacebook { CallBackUrl = Config.Urls.Home.Trim('/') + "/Register.aspx?facebook=1&login=1", Scope = "user_birthday,email" }; if (Request["code"] == null) { //Redirect the user back to Facebook for authorization. Response.Redirect(oAuth.AuthorizationLinkGet()); } else { //Get the access token and secret. oAuth.AccessTokenGet(Request["code"]); if (oAuth.Token.Length > 0) { string url = string.Format("https://graph.facebook.com/me?access_token={0}", oAuth.Token); //Response.Write(url); string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty); var userInfo = (JContainer)Newtonsoft.Json.JsonConvert.DeserializeObject(json); var userId = Convert.ToInt64(userInfo["id"].Value<string>()); #region log on the user if she is already registered string[] usernames = Classes.User.FetchUsernamesByFacebookID(new[] { userId }); if (usernames.Length > 0) { UserSession userSession; try { userSession = new UserSession(usernames[0]); Classes.User.AuthorizeByFacebookID(userId); userSession.Authorize(Session.SessionID); } catch (NotFoundException) { goto populatedata; } catch (AccessDeniedException err) { StatusPageMessage = err.Message; Response.Redirect(Config.Urls.Home + "/ShowStatus.aspx"); return; } catch (SmsNotConfirmedException) { Response.Redirect("SmsConfirm.aspx?username="******"/ShowStatus.aspx"); return; } catch (Exception err) { Global.Logger.LogWarning(err); StatusPageMessage = err.Message; Response.Redirect(Config.Urls.Home + "/ShowStatus.aspx"); return; } CurrentUserSession = userSession; CurrentUserSession.LoggedInThroughFacebook = true; Response.Redirect("Home.aspx"); } #endregion populatedata: FacebookID = userId; Session["FacebookID"] = userId; DateTime birthday; if (userInfo["birthday"] != null && DateTime.TryParse(userInfo["birthday"].Value<string>(), CultureInfo.InvariantCulture, DateTimeStyles.None, out birthday)) { datePicker1.SelectedDate = birthday; } if (userInfo["email"] != null) txtEmail.Text = userInfo["email"].Value<string>(); if (userInfo["username"] != null) txtUsername.Text = userInfo["username"].Value<string>(); User.eGender? gender = null; if (Config.Users.DisableGenderInformation) { dropGender.SelectedValue = ((int)Classes.User.eGender.Male).ToString(); if (Config.Users.InterestedInFieldEnabled) dropInterestedIn.SelectedValue = ((int)Classes.User.eGender.Male).ToString(); } else { if (userInfo["gender"] != null) { switch (userInfo["gender"].Value<string>().ToLower(CultureInfo.InvariantCulture)) { case "male": gender = Classes.User.eGender.Male; break; case "female": gender = Classes.User.eGender.Female; break; } } if (gender.HasValue) { dropGender.SelectedValue = ((int) gender).ToString(); if (Config.Users.InterestedInFieldEnabled) { switch (gender) { case Classes.User.eGender.Male: dropInterestedIn.SelectedValue = ((int) Classes.User.eGender.Female).ToString(); break; case Classes.User.eGender.Female: dropInterestedIn.SelectedValue = ((int) Classes.User.eGender.Male).ToString(); break; } } } } if (userInfo["username"] != null) PrimaryPhotoURL = string.Format("https://graph.facebook.com/{0}/picture?type=large", userInfo["username"].Value<string>()); if (userInfo["name"] != null) txtName.Text = userInfo["name"].Value<string>(); divFacebook.Visible = false; } } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Config.Misc.EnableFacebookIntegration) { var oAuth = new oAuthFacebook { CallBackUrl = Config.Urls.Home.Trim('/') + "/LoginThroughFacebook.aspx", Scope = "user_birthday,email,publish_stream" }; if (Request["code"] == null) { //Redirect the user back to Facebook for authorization. Response.Redirect(oAuth.AuthorizationLinkGet()); } else { //Get the access token and secret. oAuth.AccessTokenGet(Request["code"]); if (oAuth.Token.Length > 0) { string url = string.Format("https://graph.facebook.com/me?access_token={0}", oAuth.Token); string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty); var userInfo = (JContainer)Newtonsoft.Json.JsonConvert.DeserializeObject(json); var userId = Convert.ToInt64(userInfo["id"].Value<string>()); string[] usernames = Classes.User.FetchUsernamesByFacebookID(new[] { userId }); if (usernames.Length == 0) { Response.Redirect("Register.aspx?facebook=1&login=1"); return; } UserSession userSession; try { userSession = new UserSession(usernames[0]); Classes.User.AuthorizeByFacebookID(userId); userSession.Authorize(Session.SessionID); Facebook.SaveCredentials(usernames[0], oAuth.Token); } catch (NotFoundException) { Response.Redirect("Register.aspx?facebook=1&login=1"); return; } catch (AccessDeniedException err) { StatusPageMessage = err.Message; Response.Redirect(Config.Urls.Home + "/ShowStatus.aspx"); return; } catch (SmsNotConfirmedException) { Response.Redirect("SmsConfirm.aspx?username="******"/ShowStatus.aspx"); return; } catch (Exception err) { Global.Logger.LogWarning(err); StatusPageMessage = err.Message; Response.Redirect(Config.Urls.Home + "/ShowStatus.aspx"); return; } CurrentUserSession = userSession; CurrentUserSession.LoggedInThroughFacebook = true; Response.Redirect("Home.aspx"); } } } } }