Infinite refresh - Getting Started with the Facebook C# SDK

蹲街弑〆低调 提交于 2019-12-12 09:10:19

问题


I made the example in http://csharpsdk.org/docs/web/getting-started, and it works.
But the javascript is constantly executing, so he's constantly do a post on the server..
Always invoke the handler who Redirect to About.aspx and codebehind read the name, ID and other user information.

form.setAttribute("action", '/FacebookLogin.ashx');

In my MasterPage I have the < div id="fb-root"> and the script code right after the tag < body> (inside the body).
And in my Default.aspx I have the button to LogIn.

Can you help me?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/11898862/infinite-refresh-getting-started-with-the-facebook-c-sharp-sdk

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