Example #1
0
        /// <summary>
        ///  Gets the Facebook session from the http request.
        /// </summary>
        /// <param name="appId">
        /// The app id.
        /// </param>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="httpContext">
        /// The http context.
        /// </param>
        /// <returns>
        /// Returns the Facebook session if found, otherwise null.
        /// </returns>
        internal static FacebookSession GetSession(IFacebookApplication settings, HttpContextBase httpContext, FacebookSignedRequest signedRequest)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (string.IsNullOrEmpty(settings.AppId))
            {
                throw new Exception("settings.AppId is null.");
            }
            if (string.IsNullOrEmpty(settings.AppSecret))
            {
                throw new Exception("settings.AppSecret is null.");
            }
            if (httpContext == null)
            {
                throw new ArgumentNullException("httpContext");
            }

            // If the session is not null, we explicitly DO NOT want to
            // read from the cookie. Cookies in iFrames == BAD
            bool readSessionFromCookie = signedRequest == null;

            FacebookSession facebookSession = null;
            var             httpRequest     = httpContext.Request;
            var             items           = httpContext.Items;

            if (items[HttpContextKey] == null)
            {
                if (signedRequest == null)
                {
                    // try creating session from signed_request if exists.
                    signedRequest = FacebookSignedRequest.GetSignedRequest(settings.AppId, settings.AppSecret, httpContext);
                }

                if (signedRequest != null)
                {
                    facebookSession = FacebookSession.Create(settings, signedRequest);
                }

                if (readSessionFromCookie && facebookSession == null)
                {
                    // try creating session from cookie if exists.
                    var sessionCookieValue = GetSessionCookieValue(settings.AppId, httpRequest);
                    if (!string.IsNullOrEmpty(sessionCookieValue))
                    {
                        facebookSession = FacebookSession.ParseCookieValue(settings, sessionCookieValue);
                    }
                }

                if (facebookSession != null)
                {
                    items.Add(HttpContextKey, facebookSession);
                }
            }
            else
            {
                facebookSession = items["facebook_session"] as FacebookSession;
            }

            return(facebookSession);
        }
Example #2
0
        /// <summary>
        ///  Gets the facebook session from the http request.
        /// </summary>
        /// <param name="appId">
        /// The app id.
        /// </param>
        /// <param name="appSecret">
        /// The app secret.
        /// </param>
        /// <param name="httpContext">
        /// The http context.
        /// </param>
        /// <returns>
        /// Returns the facebook session if found, otherwise null.
        /// </returns>
        internal static FacebookSession GetSession(string appId, string appSecret, HttpContextBase httpContext, FacebookSignedRequest signedRequest)
        {
            Contract.Requires(!string.IsNullOrEmpty(appId));
            Contract.Requires(!string.IsNullOrEmpty(appSecret));
            Contract.Requires(httpContext != null);
            Contract.Requires(httpContext.Items != null);
            Contract.Requires(httpContext.Request != null);
            Contract.Requires(httpContext.Request.Params != null);

            // If the session is not null, we explicitly DO NOT want to
            // read from the cookie. Cookies in iFrames == BAD
            bool readSessionFromCookie = signedRequest == null;

            FacebookSession facebookSession = null;
            var             httpRequest     = httpContext.Request;
            var             items           = httpContext.Items;

            if (items[HttpContextKey] == null)
            {
                if (signedRequest == null)
                {
                    // try creating session from signed_request if exists.
                    signedRequest = FacebookSignedRequest.GetSignedRequest(appSecret, httpContext);
                }

                if (signedRequest != null)
                {
                    facebookSession = FacebookSession.Create(appSecret, signedRequest);

                    httpContext.Session[HttpContextKey] = facebookSession;
                }
                else if (null != httpContext.Session[HttpContextKey])
                {
                    facebookSession = httpContext.Session[HttpContextKey] as FacebookSession;
                }
                else if (!string.IsNullOrEmpty(httpContext.Request["code"]))
                {
                    facebookSession = FacebookSession.Create(appSecret, httpContext.Request["code"]);
                }

                if (readSessionFromCookie && facebookSession == null)
                {
                    // try creating session from cookie if exists.
                    var sessionCookieValue = GetSessionCookieValue(appId, httpRequest);
                    if (!string.IsNullOrEmpty(sessionCookieValue))
                    {
                        facebookSession = FacebookSession.ParseCookieValue(appSecret, sessionCookieValue);
                    }
                }

                if (facebookSession != null)
                {
                    items.Add(HttpContextKey, facebookSession);
                    httpContext.Session[HttpContextKey] = facebookSession;
                }
            }
            else
            {
                facebookSession = items["facebook_session"] as FacebookSession;
            }

            return(facebookSession);
        }