facebook登录按钮onclick事件

时间:2012-08-04 22:59:21

标签: facebook asp.net-mvc-3 login

我添加了登录按钮,如下所示,但出了点问题。前两次我获得了facebook登录窗口,但随后出现了问题。当登录窗口开始打开时它关闭。当我一步一步跟踪函数调用时,我发现onConnect()函数没有调用。有什么想法吗?

我的观点:

    <script type="text/javascript">

    $(document).ready(function () {

    if (document.getElementById('fb-root') != undefined) {
        var e = document.createElement('script');
        e.type = 'text/javascript';
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e); 
    }
});

window.fbAsyncInit = function () {
    FB.init({ appId: '455724271129246', status: true, cookie: false, xfbml: true, oauth: true });
};

function onConnect() { 
    FB.getLoginStatus(function (response) {

        if (response.session) {
            window.location = "../LogOn/FbLogin?token=" + response.session.access_token;
        } else {
            // if user cancel
        }
    });
};

    </script>

     <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
 <body>
     <div id="fb-root"></div>
    <fb:login-button perms="email,user_checkins"
        onlogin="onConnect();" autologoutlink="false">
    </fb:login-button>
</body>

我的控制员:

      public ActionResult FbLogin(string token)
    {
        WebClient client = new WebClient();
        string JsonResult = client.DownloadString(string.Concat("https://graph.facebook.com/me?access_token=", token));
        JObject jsonUserInfo = JObject.Parse(JsonResult);
        UInt64 facebook_userID = jsonUserInfo.Value<UInt64>("id");
        string username = jsonUserInfo.Value<string>("username");
        string email = jsonUserInfo.Value<string>("email");
        ViewData["email"] = email;
        return View();
    }

1 个答案:

答案 0 :(得分:2)

尝试添加订阅活动:

    FB.Event.subscribe('auth.login', function (response) {
        //do whatever
        login();
        // or onConnect(); 

    });
    FB.Event.subscribe('auth.logout', function (response) {
        //do whatever
        logout();

    });