2012-10-03 140 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