Infinite refresh - Getting Started with the Facebook C# SDK

不打扰是莪最后的温柔 提交于 2019-12-04 15:27:08

I was having this same issue as well from following the example and my issue turned out to be that on my FacebookLogin.ashx handler page, I was redirecting back to my original aspx page, which holds my JavaScript code. Upon entering this code, the JavaScript was executing my authentication again and hence putting me in an infinite loop.

What I ended up doing was grabbing a copy of some JavaScript that gives you persistent session variables in JavaScript from here. And placing a condition around the code that submits the Post. This way I only hit the FacebookLogin.ashx handler once and doesn't throw me in the loop.

Here is the code I'm using:

            FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                // the user is logged in and has authenticated your
                // app, and response.authResponse supplies
                // the user's ID, a valid access token, a signed
                // request, and the time the access token 
                // and signed request each expire
                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                if (sessvars.fbauthenticated == undefined) {
                    sessvars.fbauthenticated = "1";

                    // Do a post to the server to finish the logon
                    // This is a form post since we don't want to use AJAX
                    var form = document.createElement("form");
                    form.setAttribute("method", 'post');
                    form.setAttribute("action", '/FacebookLogin.ashx');

                    var field = document.createElement("input");
                    field.setAttribute("type", "hidden");
                    field.setAttribute("name", 'accessToken');
                    field.setAttribute("value", accessToken);
                    form.appendChild(field);

                    document.body.appendChild(form);
                    form.submit();
                }

            } else if (response.status === 'not_authorized') {
                // the user is logged in to Facebook, 
                // but has not authenticated your app
            } else {
                // the user isn't logged in to Facebook.
            }
        });

Where the conditional line of if (sessvars.fbauthenticated == undefined) { keeps my code from posting multiple times to the server.

Hope this helps

change your AppId to the actual facebook app id , which you get once you register your application

FB.init({
  appId      : 'YOUR_APP_ID', // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});

//that should solve ur issue

On another demo here some folks said it was a problem with ie9/chrome and cookies on localhost. Pointing to 127.0.0.1 and run it on iis it fixed the issue for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!