2011-12-29 25 views
0

我知道類似的問題被問了幾次,但我仍然無法使它的工作。我想用我最新的Facebook C#SDK在我的網站上登錄並閱讀登錄用戶的電子郵件和名稱。Facebook的c#sdk登錄在asp.net 4.0網站

這裏是我的web.config配置的Facebook:

<configSections> 
    <section type="Facebook.FacebookConfigurationSection, Facebook" 
    name="facebookSettings" allowLocation="true" allowDefinition="Everywhere" /> 
    </configSections> 
    <facebookSettings appId = "{appId}" appSecret = "{appSecret}"/> 

這裏是我的Facebook SDK和客戶端代碼:

Your facebook name is :<asp:Label ID="Label1" runat="server" Text="Not authenticated."></asp:Label> 

<div id="fb-root"> 
</div> 
<fb:login-button id="fblogin" onlogin="window.open('http://www.filmania.pl/Default.aspx?code=1', '_self')"> 
</fb:login-button> 
<fb:prompt-permission perms="email"></fb:prompt-permission> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: 'applicationId', 
      cookie: true, 
      xfbml: true, 
      oauth: true 
     }); 
     function facebooklogin() { 
      FB.login(function (response) { 
       if (response.authResponse) { 
        // user authorized 
        //window.location.reload(); 
        window.open("http://www.filmania.pl/Default.aspx?code=1", "_self") 
       } else { 
        // user cancelled 
       } 
      }, { scope: 'email' }); 
     }; 
    }; 
    (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 

這是我的服務器端代碼:

if (Request.QueryString["code"] == "1") 
    { 
     try 
     { 
      var fbWebContext = new FacebookWebContext(); 
      Label1.Text += fbWebContext.IsAuthenticated(); 

      if (fbWebContext.IsAuthenticated()) 
      { 
       var fb = new FacebookWebClient(); 
       dynamic me = fb.Get("me"); 
       Label1.Text += me.email + " " + me.name; 
      } 

     } 
     catch (Exception ex) 
     { 
      Label1.Text += ex; 
     } 
    } 

點擊Facebook登錄按鈕後,Facebook彈出窗口顯示,並要求許可。似乎工作正常,但服務器端代碼後神奇的Label1.Text總是==假。我在這裏錯過了什麼?請幫助我,我迷路了。

回答

0

根據JavaScript SDK,您需要調用FB.getLoginStatus 之前的來調用FB.login。這可能會改變你的工作流程。

參見: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/https://developers.facebook.com/docs/reference/javascript/FB.login/

+0

感謝您的回覆。我終於設法用FB.Login做到了。我將此代碼添加到服務器端: var app = new DefaultFacebookApplication(); app.AppId =「appIUd」; app.AppSecret =「appSecret」; var fbWebContext = new FacebookWebContext(app); 使用FB.login是否錯誤,或者我可以這樣做嗎? – user964986 2011-12-30 01:53:22

+0

好吧,我將它改爲FB.getLoginStatus及其工作方式。謝謝。 – user964986 2011-12-30 02:00:11

+0

Whe!我很高興你明白了。快樂的編碼! – DMCS 2011-12-30 03:26:55