ParseSignedRequest() public méthode

Parse the facebook signed_request.
Throws if appSecret or signedRequestValue is null or empty. If the signedRequestValue is an invalid signed_request.
public ParseSignedRequest ( string signedRequestValue ) : object
signedRequestValue string The signed_request value.
Résultat object
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!string.IsNullOrEmpty(filterContext.HttpContext.Request[_signedKey]))
            {
                var client = new FacebookClient();
                client.AppId = GetAppId();
                client.AppSecret = GetSecret();
                dynamic obj = client.ParseSignedRequest(filterContext.HttpContext.Request[_signedKey]);

                if (obj.page != null)
                {
                    bool isLiked = false;
                    isLiked = obj.page.liked;
                    if (!isLiked) return;
                }
            }

            if (string.IsNullOrEmpty(filterContext.HttpContext.Request[_statusKey]))
            {
                var resp = Resources.Filters.FacebookAuthorise;
                resp = resp.Replace(new Dictionary<string, string>()
                {
                    { "{{appId}}", this.GetAppId() },
                    { "{{permissions}}", GetPermissions() },
                    { "{{url}}", filterContext.HttpContext.Request.Url.ToString()}
                });
                filterContext.HttpContext.Response.Write(resp);
                filterContext.Result = new EmptyResult();
                return;
            }
            else if (filterContext.HttpContext.Request[_statusKey].Equals("popup-blocked"))
            {
                return;
            }
            else if (!filterContext.HttpContext.Request[_statusKey].Equals("connected"))
            {
                if (!string.IsNullOrEmpty(this.ErrorPageAction))
                {
                    var route = new RouteValueDictionary()
                        {
                            { "action", this.ErrorPageAction}
                        };
                    if (!string.IsNullOrEmpty(this.ErrorPageController))
                    {
                        route.Add("controller", this.ErrorPageController);
                    }

                    filterContext.Result = new RedirectToRouteResult(route);
                }
                else if (!string.IsNullOrEmpty(this.ErrorPageUrl))
                {
                    filterContext.HttpContext.Response.Redirect(this.ErrorPageUrl);
                }
            }
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //Authorisation request submitted.
            if (!string.IsNullOrEmpty(filterContext.HttpContext.Request["status"])) return;

            if (string.IsNullOrEmpty(filterContext.HttpContext.Request[_signedKey]))
            {
                return;
            }
            else
            {
                bool isLiked = false;

                var client = new FacebookClient();
                client.AppId = GetAppId();
                client.AppSecret = GetSecret();
                dynamic obj = client.ParseSignedRequest(filterContext.HttpContext.Request[_signedKey]);

                if (null != obj.page)
                {
                    isLiked = obj.page.liked;
                }

                if ((isLiked) || (null == obj.page))
                {
                    return;
                }
            }

            if (!string.IsNullOrEmpty(this.Action))
            {
                var route = new RouteValueDictionary()
                {
                    { "action", this.Action}
                };
                if (!string.IsNullOrEmpty(this.Controller))
                {
                    route.Add("controller", this.Controller);
                }

                filterContext.Result = new RedirectToRouteResult(route);
                return;
            }
            else if (!string.IsNullOrEmpty(this.Url))
            {
                filterContext.HttpContext.Response.Redirect(this.Url);
                return;
            }

            throw new Exception("Enter Like Url or Route Data for Facebook Like Attribute.");
        }
        public void CorrectlyParsesSignedRequest()
        {
            var fb = new FacebookClient();

            var signedRequest = (IDictionary<string, object>)fb.ParseSignedRequest(AppSecret, SignedRequest);

            Assert.IsAssignableFrom<IDictionary<string, object>>(signedRequest);
            Assert.IsType<JsonObject>(signedRequest);

            Assert.Equal("HMAC-SHA256", signedRequest["algorithm"]);
            Assert.Equal(1336845600L, signedRequest["expires"]);
            Assert.Equal(1336841938L, signedRequest["issued_at"]);
            Assert.Equal("AAAB3grfTrXwBAIYmsIDKbgepKdL6M5IK3v4pMGAi6OEKWLzX91bZBC4ZATzadiLnbK4k8CBrSbo5ZCqW5a7aZA3F5DSHMIh3WarnNVLRGTg2TWLbpJ4z", signedRequest["oauth_token"]);
        }
Exemple #4
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // On bypass completement l'accès facebook si la conf n'est pas à true.
            if (ConfigurationManager.AppSettings["FbEnabled"] != "true")
            {
                return;
            }
            var request = filterContext.RequestContext.HttpContext.Request;

            //vérifie si l'on est bien dans Facebook
            if (request.Params["signed_request"] != null)
            {
                var fb = new FacebookClient();

                dynamic sr = fb.ParseSignedRequest(ConfigurationManager.AppSettings["FbAppSecret"], request.Params["signed_request"]);
                filterContext.HttpContext.Session["signedRequest"] = sr;
                filterContext.HttpContext.Session["SignedRequestReceived"] = true;
                if (sr.page != null)
                {
                    //page is liked or not ?
                    if (!sr.page.liked && RedirectIfPageNotLiked)
                    {
                        filterContext.Result = new RedirectResult("~/");
                    }
                }
            }
            else if (filterContext.HttpContext.Session["SignedRequestReceived"] != null)
            {
                filterContext.HttpContext.Session["SignedRequestReceived"] = false;
                return;
            }
            else
            {
                filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings["FbAppRoot"]);
            }
            base.OnActionExecuting(filterContext);
        }
        private dynamic GetFacebookUserData(string _signedRequest)
        {
            FacebookClient client = new FacebookClient();
            dynamic signedRequest = client.ParseSignedRequest(ConfigurationManager.AppSettings["facebook_appsecret"].ToString(), _signedRequest);

            client.AppId = ConfigurationManager.AppSettings["facebook_appid"].ToString();
            client.AppSecret = ConfigurationManager.AppSettings["facebook_appsecret"].ToString();

            if (signedRequest.ContainsKey("oauth_token"))
                client.AccessToken = (string)signedRequest["oauth_token"];
            else
                return null;

            dynamic response = client.Get("me");
            return response;
        }
        public void ThrowsArgumentNullExceptionIfSignedRequestIsEmpty()
        {
            var fb = new FacebookClient();

            Assert.Throws<ArgumentNullException>(() => fb.ParseSignedRequest(AppSecret, string.Empty));
        }
        public void ThrowsArgumentNullExceptionIfAppSecretIsNull()
        {
            var fb = new FacebookClient();

            Assert.Throws<ArgumentNullException>(() => fb.ParseSignedRequest(null, SignedRequest));
        }