无限刷新 - Facebook C#SDK入门

时间:2012-08-10 09:27:52

标签: facebook-c#-sdk

我在http://csharpsdk.org/docs/web/getting-started中做了这个例子,它有效 但是javascript一直在执行,所以他经常在服务器上发帖子。
始终调用重定向到About.aspx并且代码隐藏的处理程序读取名称,ID和其他用户信息。

  

form.setAttribute(“action”,“/ FriebookLogin.ashx”);

在我的MasterPage中我有< div id =“fb-root”>和标签后面的脚本代码<体> (在体内)。
在我的Default.aspx中,我有按钮登录。

你能帮助我吗?

3 个答案:

答案 0 :(得分:2)

我也遇到了同样的问题,并且在我的FacebookLogin.ashx处理程序页面上发现了我的问题,我正在重定向回原来的aspx页面,该页面保存了我的JavaScript代码。输入此代码后,JavaScript再次执行我的身份验证,从而使我处于无限循环中。

我最终做的是抓取一些JavaScript的副本,这些JavaScript在here的JavaScript中为您提供持久的会话变量。并在提交Post的代码周围放置一个条件。这样我只打了一次FacebookLogin.ashx处理程序,并没有把我扔进循环中。

以下是我正在使用的代码:

            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.
            }
        });

if(sessvars.fbauthenticated == undefined){的条件行使我的代码不会多次发布到服务器。

希望这有帮助

答案 1 :(得分:0)

将您的AppId更改为您注册申请后获得的实际Facebook应用ID

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
});

//应该解决你的问题

答案 2 :(得分:0)

在另一个演示here上,有些人说本地主机上有ie9 / chrome和cookies的问题。指向127.0.0.1并在iis上运行它为我解决了问题。