2013-02-23 222 views
0

在此處輸入代碼我一直在關注C#的Facebook教程並獲取訪問令牌,我剛剛得到了它,但似乎無法理解其教程中關於事件處理和重定向。我從來沒有做過類似的JavaScript使用C#。我有以下幾點:Facebook登錄後重定向C#

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '{app Id}', // 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", '/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') { 
       // 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> 

除了作爲一個Facebook的按鈕,一切正常找到,但隨後,根據教程Here我需要「接下來,創建一個頁面,操作或處理接收的令牌和重定向在這個例子中,我們將創建一個通用處理程序。「

他們用這樣的:

public class FacebookLogin : IHttpHandler, System.Web.SessionState.IRequiresSessionState { 

    public void ProcessRequest(HttpContext context) { 
     var accessToken = context.Request["accessToken"]; 
     context.Session["AccessToken"] = accessToken; 

     context.Response.Redirect("/MyUrlHere"); 
    } 

    public bool IsReusable { 
     get { return false; } 
    } 
} 

我只是把在我後面的代碼,但它沒有做任何事情。我只是不確定要從這裏做什麼。如何讓我的網頁重定向到一個新的頁面,所以我可以做這樣的事情:

var accessToken = Session["AccessToken"].ToString(); 
var client = new FacebookClient(accessToken); 
dynamic result = client.Get("me", new { fields = "name,id" }); 
string name = result.name; 
string id = result.id; 
+0

做了一些測試,它看起來像FB.Event.subscribe後(「auth.authResponseChange」,函數(響應)從來沒有得到調用。我錯過了什麼? – KJ3 2013-02-23 22:14:21

+0

你是否得到這個工作即時通訊工作現在,但像你自己不知道如何登錄用戶回來,當他們修改網站 – rogue39nin 2015-12-14 14:14:04

回答

0

確保你不把FacebokLogin處理器在aspx.cs代碼隱藏文件。它需要是一個單獨的.ashx文件。在你的評論中,你說FB.Event.subscribe('auth.authResponseChange',function(response){...}永遠不會被調用,嘗試在FB中包含apiKey:「[OAuth token goes here]」。初始化。不知道這是否會工作,但它是值得一試。

FB.init({ 
    appId: '{app Id}', // App ID 
    status: true, // check login status 
    cookie: true, // enable cookies to allow the server to access the session 
    xfbml: true, // parse XFBML 
    apiKey: "92834yv221985vn4139y845vn19835vynv519835vn" 
});