Ejemplo n.º 1
0
        public static void LoadFBMLPage(FacebookService fbService, bool autoAdd, HttpRequest request, HttpResponse response)
        {
            string sessionKey = null;
            string userId     = null;
            string inCanvas   = request[REQUEST_IN_CANVAS];

            fbService.IsDesktopApplication = false;

            if (string.IsNullOrEmpty(fbService.SessionKey) || string.IsNullOrEmpty(fbService.UserId))
            {
                sessionKey = request[REQUEST_SESSION_KEY];
                userId     = request[REQUEST_USER_ID];
            }
            else
            {
                sessionKey = fbService.SessionKey;
                userId     = fbService.UserId;
            }

            // When the user uses the facebook login page, the redirect back here will will have the auth_token in the query params
            string authToken = request.QueryString[QUERY_AUTH_TOKEN];

            // We have already established a session on behalf of this user
            if (!String.IsNullOrEmpty(sessionKey))
            {
                fbService.SessionKey = sessionKey;
                fbService.UserId     = userId;
            }
            //// This will be executed when facebook login redirects to our page
            else if (!String.IsNullOrEmpty(authToken))
            {
                fbService.CreateSession(authToken);
            }
            else
            {
                //response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0");
                response.Write("<fb:redirect url=\"" + FACEBOOK_LOGIN_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
                response.End();
            }

            if (!fbService.IsAppAdded() && autoAdd)
            {
                if ((!string.IsNullOrEmpty(inCanvas) && inCanvas.Equals("1")) || (!string.IsNullOrEmpty(request.Url.ToString()) && request.Url.ToString().ToLower().Contains("facebook.com")))
                {
                    response.Write("<fb:redirect url=\"" + FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0" + "\"/>");
                    response.End();
                }
                else
                {
                    response.Redirect(FACEBOOK_ADD_URL + fbService.ApplicationKey + @"&v=1.0");
                }
            }
        }
Ejemplo n.º 2
0
        private void EstablishSession(string sessionKey, string userId, string authToken, bool retry)
        {
            try
            {
                object sessionKeyFromSession = _useSession ? _session[SESSION_SESSION_KEY] :
                                               _request.Cookies[SESSION_SESSION_KEY];

                if (String.IsNullOrEmpty(sessionKey) && sessionKeyFromSession != null)
                {
                    sessionKey = _useSession ? sessionKeyFromSession.ToString() : ((HttpCookie)sessionKeyFromSession).Value;
                    userId     = _useSession ? _session[SESSION_USER_ID].ToString() : _request.Cookies[SESSION_USER_ID].Value;
                }

                if (!String.IsNullOrEmpty(sessionKey))
                {
                    _fbService.SessionKey = sessionKey;
                    _fbService.UserId     = userId;
                    SetSessionInfo(sessionKey, userId);
                }
                //// This will be executed when facebook login redirects to our page
                else if (!String.IsNullOrEmpty(authToken))
                {
                    _fbService.CreateSession(authToken);
                    SetSessionInfo(_fbService.SessionKey, _fbService.UserId);
                }
                else
                {
                    RedirectToLogin();
                    return;
                }

                TryAddApp();
            }
            catch (Facebook.Exceptions.FacebookTimeoutException)
            {
                if (retry)
                {
                    ClearSessionInfo();
                    EstablishSession(null, null, authToken, false);
                }
                else
                {
                    throw;
                }
            }
        }