2012-10-03 43 views
2

這是在我的網頁腳本:Facebook登錄永遠重新加載頁面

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '419349611446911', // App ID 
      status: true, // check login status 
      cookie: true, // enable cookies to allow the server to access the session 
      xfbml: true // parse XFBML 
     }); 

     // Additional initialization code here 
     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; 
       // TODO: 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 form = document.createElement("form"); 
       form.setAttribute("method", 'post'); 
       form.setAttribute("action", '@Url.Action("FacebookLogin", "Account")'); 

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

    // Load the SDK Asynchronously 
    (function (d) { 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) { return; } 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     ref.parentNode.insertBefore(js, ref); 
    } (document)); 
</script> 

這裏是我:LogOnPartial

@if(Request.IsAuthenticated) { 
    <text>Hola, <strong>@User.Identity.Name</strong>! 
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text> 
} 
else { 
    <div class="fb-login-button" data-show-faces="false" data-width="200" data-max-rows="1"></div> 
} 

當我更改爲auth.login點擊登錄按鈕不登錄甚至在任何地方重定向。只是一小部分的加載,沒有別的。當我使用auth.authReponseChange時,我可以使用Facebook正確登錄,但頁面會一遍又一遍地重新加載。

我在問什麼,我該如何解決這個錯誤?爲什麼每次都會觸發auth.authResponseChange事件?

例子:

我登出我的本地網站:

FormsAuthentication.SignOut(); 

即使如此,Facebook的按鈕自動登錄我,並繼續循環。什麼是點擊登錄按鈕點擊?

回答

2

請嘗試更改事件從FB.Event.subscribe(「auth.authResponseChange」訂閱 「auth.login」,這將觸發你的代碼僅在第一次時,用戶登錄界面,在。

下面是採取from fb documentations

auth.login 該事件被觸發時,你的應用程序第一次注意到用戶(換句話說,獲得一個會話時,尚未有有效的一個)。

AU th.logout 當您的應用程序發現不再有效的用戶(換句話說,它有會話但無法再驗證當前用戶)時,會觸發此事件。

auth.authResponseChange 這個事件對於任何身份驗證有關的變化,因爲它們都影響會話:登錄,註銷,會話刷新。只要用戶對您的應用程序處於活動狀態,會話就會隨着時間的推移而刷新。

auth.statusChange 通常您會希望使用auth.authResponseChange事件。但是,在極少數情況下,你想這三種狀態來區分:

  • 連接已
  • 登錄到Facebook上,但不與你的
  • 應用程序沒有登錄到Facebook的所有連接。
+0

請檢出此代碼http://jsfiddle.net/HUSHj/ – thomasbabuj

+0

由於某種原因,現在沒有任何事情發生。代碼流甚至沒有進入'auth.login'事件處理程序。 – sergserg

+0

更改爲auth.login後,是否發生這種情況? – thomasbabuj