Hello everyone, I am trying to implement Facebook login/account creation.
My login page redirects to Facebook and receives a proper response. However it seems to get stuck on the FBCallback page and hangs up and I can't figure out why....
This is my FBCallback.aspx.cs
protected void Page_Load(object sender, EventArgs e) { string url = ""; // cache the Request code, since LINQ does not evaluate at runtime properly string authCode = Request["code"]; oAuthFacebook oAuth = new oAuthFacebook(); if (authCode == null) { //Redirect the user back to Facebook for authorization. Response.Redirect(oAuth.AuthorizationLinkGet()); } else { //Get the access token and secret. oAuth.AccessTokenGet(authCode); if (oAuth.Token.Length > 0) { Lit1.Text += "0"; //We now have the credentials, so we can start making API calls url = "https://graph.facebook.com/me/?access_token=" + oAuth.Token; string json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty); if (json != null) { Lit1.Text += ""; // parse the response object first, we may want to create a user from it. JObject o = JObject.Parse(json); string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["MembershipConnection"].ConnectionString; using (SqlConnection con = new SqlConnection(connstring)) { con.Open(); bool exists = false; string username = (string)o["first_name"] + " " + (string)o["last_name"]; string email = (string)o["email"]; // create a command to check if the username exists using (SqlCommand cmd = new SqlCommand("select count(*) from Users inner join memberships on users.userid=memberships.userid where UserName=@UserName and email=@email", con)) { cmd.Parameters.AddWithValue("UserName", username); cmd.Parameters.AddWithValue("email", email); exists = (int)cmd.ExecuteScalar() > 0; } // if exists, show a message error if (exists) FormsAuthentication.SetAuthCookie(username, true); else { //Create the new user } con.Close(); } } } // if our token was blank we have failure else { Lit1.Text = "blank"; } Response.Redirect("/"); } }
Thanks!