Exemple #1
0
        public void TryLoginThroughFacebook(LoveHitchFacebookApp facebook, long id, string accessToken, string backUrl)
        {
            FacebookHelper.SessionFacebookApp = facebook;
            FacebookHelper.CurrentSessionAccessToken = accessToken;
            FacebookHelper.CurrentSessionUserID = id;
            FacebookHelper.IsCurrentSessionConnected = true;
            FacebookHelper.SessionConnectTriesElapsed = 0;

            SetCurrentUserSession(null);
            string[] usernames = null;
            UserSession userSession;

            try
            {
                usernames = Data.User.AuthorizeByFacebookID(id);
                userSession = new UserSession(usernames[0]);
                ((PageBase)Page).CurrentUserSession = userSession;
                MatchmakerHelper.CurrentUsername = userSession.Username;
                userSession.Authorize(Session.SessionID);
                userSession.LoggedInThroughFacebook = true;
                //facebook.Session.UserId = id;
                userSession.SetFacebookApp(facebook);
                //Response.Redirect(backUrl.ToLower().Contains("loginthroughfacebook.aspx") ? "home.aspx" : backUrl);
                userSession.ToUpdateFacebookFriendsData = true;
            }
            catch (NotFoundException)
            {
                if (CurrentUserSession != null)
                {
                    CurrentUserSession.FacebookID = id;
                    CurrentUserSession.Update();
                    MatchmakerHelper.CurrentUsername = CurrentUserSession.Username;
                    if (!CurrentUserSession.IsAuthorized) CurrentUserSession.Authorize(Session.SessionID);
                    CurrentUserSession.LoggedInThroughFacebook = true;
                    if (CurrentUserSession.FacebookID.HasValue && CurrentUserSession.FacebookID > 0)
                    {
                        var qry = "SELECT uid1, uid2 FROM friend " +
                            " WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=me()) " +
                            " AND   uid2 IN (SELECT uid2 FROM friend WHERE uid1=me())";
                        JsonArray mutualFriendsByQuery = FacebookHelper.GetFqlResult(qry);
                        Session["FacebookMutualFriends"] =
                            mutualFriendsByQuery
                                .Select(o => new MutualFriendItem
                                {
                                    FriendID = long.Parse((string)((JsonObject)o)["uid1"]),
                                    MutualFriendID = long.Parse((string)((JsonObject)o)["uid2"])
                                })
                                .ToList();
                        FacebookHelper.AddFriends(CurrentUserSession, 0, HttpContext.Current, null);
                        Response.Redirect(backUrl ?? MatchmakerHelper.CurrentHomePage);
                        //Server.Transfer(backUrl ?? MatchmakerHelper.CurrentHomePage);
                    }
                }
                else
                {
                    Response.Redirect("Registration.aspx?facebook=1&back_url=" + backUrl);
                    //Server.Transfer("Registration.aspx?facebook=1&back_url=" + backUrl);
                    return;
                }
            }
            catch (AccessDeniedException err)
            {
                IDictionary<string, object> parameters = new Dictionary<string, object>();
                StatusPageMessage = err.Message;
                parameters["next"] = "ShowStatus.aspx";
                //Response.Redirect(facebook.GetLogoutUrl(parameters).ToString());
                Response.Redirect("ShowStatus.aspx");
                //Server.Transfer("ShowStatus.aspx");
                return;
            }
            catch (SmsNotConfirmedException)
            {
                Response.Redirect("SmsConfirm.aspx?username="******"next"] = Config.Urls.Home + "/ShowStatus.aspx";
                //Response.Redirect(facebook.GetLogoutUrl(parameters).ToString());
                Response.Redirect("ShowStatus.aspx");
                //Server.Transfer("ShowStatus.aspx");
                return;
            }
            catch (Exception err)
            {
                IDictionary<string, object> parameters = new Dictionary<string, object>();
                Global.Logger.LogWarning(err);
                StatusPageMessage = err.Message;
                parameters["next"] = Config.Urls.Home + "/ShowStatus.aspx";
                Response.Redirect("ShowStatus.aspx");
                //Server.Transfer("ShowStatus.aspx"); 
                return;
            }
            //Response.Redirect(backUrl.ToLower().Contains("loginthroughfacebook.aspx") ? "default.aspx" : backUrl);
            Response.Redirect(MatchmakerHelper.CurrentHomePage);
            //Response.Redirect("ManageProfile.aspx");
            //Server.Transfer("default.aspx", false);
        }