2012-05-01 43 views
1

我是新来的Facebook应用程序,最近阅读可用的文档。然后,我开始从NuGet获取Facebook C#SDK并阅读get started tutorial简单登录Facebook C#SDK不能正常工作

我修改了一下。我有3页:

  • Default.aspx的:有“与Facebook登录”按钮
  • Protected.aspx:我最终想成为没有先注销不可访问的
  • DoSignIn的页面。 aspx:包含对令牌的请求并重定向回Default.aspx

  • 在Default.aspx上,我有一个按钮将我重定向到Protected.aspx页面。在它上面,我有一个标签给我看令牌。只有

    • 当我点击“与Facebook登录”按钮,该按钮并没有改变,以表明我登录它:

    现在,我的问题是这样的。显示如果我在页面上进行刷新,我已登录。

  • 如果我点击按钮将我重定向到Protected.aspx页面,我看不到标签中的标记,因为DoSignIn.aspx页面从未被调用过。

如果有人能指出我做错了什么(因为我几乎复制了教程示例),我将非常感激。我花了3个小时摆弄这件事,并且搜索Google和所有的东西,不知道在哪里搜索。

谢谢你的时间。

PS。我有最新的Facebook C#SDK和App ID似乎做工精细,当我手动刷新我的浏览器,我确实在登录

我添加了代号为3页:

的Default.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <div id="fb-root"> 
    </div> 
    <script type="text/javascript"> 
     window.fbAsyncInit = function() { 
      FB.init({ 
       appId: 'I_INSERTED_MY_APP_ID_HERE', // App ID 
       status: true, // check login status 
       cookie: true, // enable cookies to allow the server to access the session 
       xfbml: true // parse XFBML 
      }); 


      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; 

        // - 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", 'DoSignIn.aspx'); 

        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> 
    <form id="form1" runat="server"> 
    <div> 
     <div class="fb-login-button" data-show-faces="true" data-width="400" data-max-rows="1"> 
      Log in with Facebook</div> 
     <br /> 
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to protected" /> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("Protected.aspx"); 
    } 
} 

DoSignIn.aspx.cs:

public partial class DoSignIn : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var accessToken = Request["accessToken"]; 
     Session["AccessToken"] = accessToken; 

     Response.Redirect("Default.aspx"); 
    } 
} 

Protected.aspx.cs:

public partial class Protected : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["AccessToken"] != null) 
     { 
      Label1.Text = Session["AccessToken"].ToString(); 
     } 
     else 
     { 

      Label1.Text = "Not authenticated"; 
     } 
    } 
+0

当您加载默认页面时,是否收到任何Javascript错误? – shashi

+0

不,没有javascript错误。 – ilovebigmacs

回答

1

对,我花了几个小时寻找,终于找到了为什么它不工作。它与上面的代码无关,但带有Facebook bug。实际上,很多人似乎无法在会话刷新时触发auth.authResponseChange。我在FB上发现了一些描述完整问题的bug报告,其中一个是here

我在开发/ localhost /时遇到了这个问题。我已经转向另一种整合Facebook登录的方式,在删除上述示例之前,我尝试将其上载到实际托管服务器上的域名下。由于我无法解释的原因,它从那里起作用。

无论如何,我正在追求的Facebook登录的实现解释为here