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!