TryParseSignedRequest() 개인적인 메소드

private TryParseSignedRequest ( string signedRequestValue, object &signedRequest ) : bool
signedRequestValue string
signedRequest object
리턴 bool
예제 #1
0
        private void context_AuthenticateRequest(object sender, EventArgs e)
        {
            var httpApplication = (HttpApplication)sender;
            var context = httpApplication.Context;

            foreach (var regex in ExcludePathsRegEx)
            {
                if(new Regex(regex).IsMatch(context.Request.Path))
                    return;
            }

            var applicationId = ConfigurationManager.AppSettings["facebookApplicationId"];
            var applicationSecret = ConfigurationManager.AppSettings["facebookApplicationSecret"];

            var client = new FacebookClient();

            //Read the signed request added by the Javascript SDK login
            var signedRequestCookie = context.Request.Cookies[string.Concat("fbsr_", applicationId)];

            dynamic signedRequest;

            if (signedRequestCookie == null)
                return;

            if (!client.TryParseSignedRequest(applicationSecret, signedRequestCookie.Value, out signedRequest))
                return;

            if (signedRequest == null)
                return;

            if (signedRequest.expires != null)
            {
                int expires = signedRequest.expires;
                return;
            }

            string userId = signedRequest.user_id;

            if (string.IsNullOrEmpty(userId))
                return;

            User user;

            try
            {
                user = User.Load(client, userId);

                if (user == null)
                    return;
            }
            catch (FacebookOAuthException ex)
            {
                return;
            }

            context.User = new GenericPrincipal(user, null);
        }
        private static void RegisterFacebookServices(IKernel kernel)
        {
            kernel.Bind<IMyFacebookAppSettings>()
                .ToConstant(new MyFacebookAppSettings
                                {
                                    AppId = ""
                                    AppSecret = ""
                                    CanvasPageName = ""  /* only the canvas page name and not the full url */
                                    CanvasUrl = "http://localhost:2408/",
                                    SecureCanvasUrl = "https://localhost:44300/"
                                })
                .InSingletonScope();

            kernel.Bind<IMyFacebookCanvasContext>()
                .ToMethod(
                    ctx =>
                    {
                        var fb = new FacebookClient();

                        var request = HttpContext.Current.Request;
                        if (request.UrlReferrer != null)
                        {
                            fb.IsSecureConnection = request.UrlReferrer.Scheme == "https";
                            fb.UseFacebookBeta = request.UrlReferrer.Host == "apps.beta.facebook.com";
                        }

                        var canvasContext = new MyFacebookCanvasContext
                                                {
                                                    AppSettings = ctx.Kernel.Get<IMyFacebookAppSettings>(),
                                                    FacebookClient = fb
                                                };

                        object signedRequest;
                        if (fb.TryParseSignedRequest(canvasContext.AppSettings.AppSecret, HttpContext.Current.Request["signed_request"], out signedRequest))
                        {
                            canvasContext.SignedRequest = new MyFacebookSignedRequest(signedRequest);
                            if (!string.IsNullOrWhiteSpace(canvasContext.SignedRequest.AccessToken))
                            {
                                fb.AccessToken = canvasContext.SignedRequest.AccessToken;

                                try
                                {
                                    dynamic result = fb.Get("me/permissions");
                                    canvasContext.Permissions = result.data[0].Keys as IEnumerable<string>;
                                }
                                catch (FacebookOAuthException)
                                {
                                    // log exception
                                    // oauth token expired or invalid
                                    canvasContext.SignedRequest = null;
                                    fb.AccessToken = null;
                                }
                            }
                        }

                        return canvasContext;
                    })
                .InRequestScope();

            kernel.Bind<FacebookClient>()
                .ToMethod(ctx => ctx.Kernel.Get<IMyFacebookCanvasContext>().FacebookClient)
                .InRequestScope();
        }