FB.logout与服务器端代码会话清理

时间:2014-04-18 12:24:05

标签: javascript asp.net facebook session facebook-c#-sdk

我需要在从Facebook JS sdk调用FB.logout后触发我的服务器端代码。这是我的html外观

<asp:LinkButton ID="lbtnSignOut" runat="server" Text="Sign Out" OnClientClick="return Logout();" OnClick="lbtnSignOut_Click"></asp:LinkButton>

和js代码

function Logout() {
        var currentToken = "<%= Session["AccessToken"]%>";

        if (currentToken != null && currentToken != '') {

            FB.logout(function (response) {                    
            });
        }

        return true;
    }

随后在服务器端代码中,我清除所有特定于应用程序的会话,并使用FormsAuthentication签出呼叫签名用户并重定向到不同的页面。

现在问题是,当我从js函数返回 true 时,服务器端代码会在不等待fb.logout完成调用的情况下触发,并且用户令牌不会过期,用户是自动重新登录 FB.Event.subscribe(&#39; auth.authResponseChange&#39;,功能(响应){}; 调用我从标准代码中选择的页面加载。< / p>

FB.Event.subscribe('auth.authResponseChange', function (response) {
            if (response.status === 'connected') {
                //SUCCESS

                //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 currentToken = "<%= Session["AccessToken"] %>";


                if (currentToken == null || currentToken == '') {

                    // Handle the access token
                    // Do a post to the server to finish the logon
                    // This is a form post since we don't want to use AJAX
                    var accessToken = response.authResponse.accessToken;

                    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') {
                //FAILED
                console.log('User cancelled login or did not fully authorize.');
            }
            else {
                //UNKNOWN ERROR
                console.log('Logged Out.');
            }
        });
    };

如果我将值设置为 false ,则用户从Facebook注销,但我的自定义代码无法启动。

所以我的问题是如何在facebook js sdk注销代码被解雇后调用服务器端代码?

任何帮助或指示将非常感谢!!!

Paritosh

1 个答案:

答案 0 :(得分:0)

将return返回true放在function(response){}块中,这样只有在FB.logout调用返回时才会调用

相关问题