无限刷新-Facebook C#SDK入门 [英] Infinite refresh - Getting Started with the Facebook C# SDK

查看:79
本文介绍了无限刷新-Facebook C#SDK入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://csharpsdk.org/docs/web/getting-started中进行了示例,并且有效.
但是javascript一直在执行,因此他一直在服务器上做文章..
始终调用重定向到About.aspx并在代码背后读取名称,ID和其他用户信息的处理程序.

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

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

在我的母版页中,我有<标记<后面紧跟着脚本代码div id ="fb-root">和脚本代码.身体>(在身体内部).
在我的Default.aspx中,有用于登录的按钮.

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.

你能帮我吗?

推荐答案

通过遵循示例,我也遇到了同样的问题,而我的问题却出在我的FacebookLogin.ashx处理程序页面上,我被重定向回我原来的aspx页面,其中包含我的JavaScript代码.输入此代码后,JavaScript再次执行了身份验证,因此使我陷入无限循环.

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.

我最终要做的是从此处获取一些JavaScript的副本,该副本为您提供了JavaScript中的持久会话变量. .并在提交该帖子的代码周围放置一个条件.这样,我只打过一次FacebookLogin.ashx处理程序,不会让我陷入循环.

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

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

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

希望这会有所帮助

这篇关于无限刷新-Facebook C#SDK入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆