2013-02-23 224 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" 
});