7

In my web application i added an option to register/login using Facebook, it works fine and does it job, i'm working with VS2013 coding in C# and ASP.NET and when i start debugging and press the button it connects to Facebook and fetches all the needed info.

The problems start when i start debugging and press the Facebook button only after a while, i already understand it's some kind of timeout, but i have no idea how to work it out.

I have this piece of code in my Page_Load on my master page:

 System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
        FaceBookConnect.API_Key = "**************";
        FaceBookConnect.API_Secret = "**************************";
        if (!IsPostBack)
        {
            if (Request.QueryString["error"] == "access_denied")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('User has denied access.')", true);
                return;
            }

            string code = Request.QueryString["code"];
            if (!string.IsNullOrEmpty(code))
            {
                string data = FaceBookConnect.Fetch(code, "me");
                FaceBookUser faceBookUser = new JavaScriptSerializer().Deserialize<FaceBookUser>(data);
                faceBookUser.PictureUrl = string.Format("https://graph.facebook.com/{0}/picture?width=9999", faceBookUser.Id);
                string fname = faceBookUser.Name.Substring(0, faceBookUser.Name.IndexOf(' '));
                string lname = faceBookUser.Name.Substring(faceBookUser.Name.IndexOf(' ') + 1);
                string email = faceBookUser.Email + "@FB";
                string pictureurl = faceBookUser.PictureUrl;
                string gender = faceBookUser.Gender;
                if (SQLDB.CheckMailExists(email))
                {
                    FormsAuthentication.SetAuthCookie(email, true);
                    FormsAuthentication.RedirectFromLoginPage(email, true);
                }
                else
                {
                    SQLDB.AddFBUser(fname, lname, email, gender, pictureurl);
                    FormsAuthentication.SetAuthCookie(email, true);
                    FormsAuthentication.RedirectFromLoginPage(email, true);
                }
            }
        }

And this code int the 'Click' code of the 'Login with Facebook' button:

FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split('?')[0]); 

I tried to move all the code concerning the Facebook connect from the Page_Load to the Click action so it will try and connect only when the user presses the button but for some reason it doesn't work...

I'm not really sure what to do since the code was something i took from the web and wasn't made by me, hope you could tell me how to refresh or connect to the Facebook servers only when it needs to.. Thanks!

2
  • 1
    What third party SDK are you using? Have you read the documentation on how it works? I would recommend using a documented SDK and understand its workings such as facebooksdk.net Your code snippets do not provide enough information about how you are implementing the login or where exactly the issue is happening.
    – Hey Darren
    Commented Apr 17, 2015 at 15:52
  • Maybe you can add a link from which you have got the code?
    – DJmRek
    Commented Apr 27, 2015 at 12:04

1 Answer 1

0

If you suspect that your Facebook connection was timed out, you should just handle it as any other exception and redirect to your login page again. Here is my code:

catch (Exception ex)
            {
                if (ex is AggregateException && ex.InnerException is Facebook.FacebookOAuthException && usedState != null && HttpContext.Current != null)
                {
                    HttpContext.Current.Response.Redirect("~/FacebookLogin.aspx?logout=true");
                }
                else
                {
                    throw;
                }
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.