【问题标题】:Facebook Connect and ASP.NETFacebook 连接和 ASP.NET
【发布时间】:2010-09-24 07:03:25
【问题描述】:

我在此处找到的身份验证概述的第 8 步:http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works

特别是,用户已通过 Facebook Connect 登录到 facebook,并且他们的网络会话已创建。如何使用 facebook 开发者工具包 v2.0(从清晰起见)来检索有关用户的信息。例如,我想获取用户的名字和姓氏。

文档中的示例适用于 facebook 应用程序,但事实并非如此。

更新

Facebook 最近发布了 Graph API。除非您正在维护使用 Facebook Connect 的应用程序,否则您应该查看最新的 API:http://developers.facebook.com/docs/

【问题讨论】:

标签: asp.net facebook facebooktoolkit


【解决方案1】:

Facebook Connect 实际上并不太难,只是缺少文档。

从这里放置必要的javascript:http://tinyurl.com/5527og

验证 cookie 是否与 facebook 提供的签名相匹配,以防止黑客入侵,请参阅:http://tinyurl.com/57ry3s 了解如何开始

创建一个 api 对象 (Facebook.API.FacebookAPI) 在 api 对象上,设置 Facebook 在创建应用程序时为您提供的应用程序密钥和秘密。 从 facebook connect 为您创建的 cookie 中设置 api.SessionKeyapi.UserId

完成后,您就可以开始调用 facebook:

Facebook.Entity.User user = api.GetUserInfo();   //will get you started with the authenticated person

【讨论】:

  • 请注意,此答案中与 facebook 相关的链接现已失效,希望有人能找到合适的替代品。
【解决方案2】:

一旦用户使用 Facebook Connect 登录,我在弄清楚如何进行服务器端调用时遇到了很多麻烦。关键是一旦成功登录,Facebook Connect javascript 就会在客户端上设置 cookie。您使用这些 cookie 的值在服务器上执行 API 调用。

令人困惑的部分是查看他们发布的 PHP 示例。他们的服务器端 API 会自动负责读取这些 cookie 值并设置一个 API 对象,以便代表登录用户发出请求。

这是一个在用户使用 Facebook Connect 登录后在服务器上使用 Facebook Toolkit 的示例。

服务器代码:

API api = new API();
api.ApplicationKey = Utility.ApiKey();
api.SessionKey = Utility.SessionKey();
api.Secret = Utility.SecretKey();
api.uid = Utility.GetUserID();

facebook.Schema.user user = api.users.getInfo();
string fullName = user.first_name + " " + user.last_name;

foreach (facebook.Schema.user friend in api.friends.getUserObjects())
{
   // do something with the friend
}

实用程序.cs

public static class Utility
{
    public static string ApiKey()
    {
        return ConfigurationManager.AppSettings["Facebook.API_Key"];
    }

    public static string SecretKey()
    {
        return ConfigurationManager.AppSettings["Facebook.Secret_Key"];
    }

    public static string SessionKey()
    {
        return GetFacebookCookie("session_key");
    }

    public static int GetUserID()
    {
        return int.Parse(GetFacebookCookie("user"));
    }

    private static string GetFacebookCookie(string name)
    {
        if (HttpContext.Current == null)
            throw new ApplicationException("HttpContext cannot be null.");

        string fullName = ApiKey() + "_" + name;
        if (HttpContext.Current.Request.Cookies[fullName] == null)
            throw new ApplicationException("Could not find facebook cookie named " + fullName);
        return HttpContext.Current.Request.Cookies[fullName].Value;
    }
}

【讨论】:

    【解决方案3】:

    我跟进了这个概念并写了一篇完整的文章,在 ASP.NET 中解决了这个问题。请参阅以下内容。

    How to Retrieve User Data from Facebook Connect in ASP.NET - Devtacular

    感谢 Calebt 为帮助程序类提供了一个良好的开端。

    享受吧。

    【讨论】:

    • 你的帖子不错,可惜图片链接坏了
    【解决方案4】:

    到目前为止列出的答案中缺少此内容:

    登录成功后,Facebook 建议您验证 cookie 实际上是合法的,并由它们放置在客户端计算机上。

    这里有两种方法可以一起使用来解决这个问题。您可能希望将 IsValidFacebookSignature 方法添加到 calebt 的 Utility 类。请注意,我也稍微更改了他的 GetFacebookCookie 方法。

    private bool IsValidFacebookSignature()
    {
            //keys must remain in alphabetical order
            string[] keyArray = { "expires", "session_key", "ss", "user" };
            string signature = "";
    
            foreach (string key in keyArray)
                signature += string.Format("{0}={1}", key, GetFacebookCookie(key));
    
            signature += SecretKey; //your secret key issued by FB
    
            MD5 md5 = MD5.Create();
            byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(signature.Trim()));
    
            StringBuilder sb = new StringBuilder();
            foreach (byte hashByte in hash)
                sb.Append(hashByte.ToString("x2", CultureInfo.InvariantCulture));
    
            return (GetFacebookCookie("") == sb.ToString());
        }
    
        private string GetFacebookCookie(string cookieName)
        {
            //APIKey issued by FB
            string fullCookie = string.IsNullOrEmpty(cookieName) ? ApiKey : ApiKey + "_" + cookieName;
    
            return Request.Cookies[fullCookie].Value;
        }
    

    SecretKey 和 ApiKey 是 Facebook 提供给您的值。在这种情况下,需要设置这些值,最好来自 .config 文件。

    【讨论】:

      【解决方案5】:

      我跟进了比尔的精彩文章,并制作了这个小组件。它负责从 Facebook Connect cookie 中识别和验证用户。

      Facebook Connect Authentication for ASP.NET

      希望对大家有所帮助!

      干杯,

      亚当

      【讨论】:

        【解决方案6】:

        我的两分钱:一个非常简单的项目,利用“用 Facebook 登录”功能 - facebooklogin.codeplex.com

        不是一个库,而是展示它是如何工作的。

        【讨论】:

          【解决方案7】:

          您也可以使用SocialAuth.NET

          它提供身份验证、个人资料和与 facebook、google、MSN 和 Yahoo 的联系,而开发工作很少。

          【讨论】:

          • 知道在哪里可以获得有关 SocialAuth.NET 的详细文章吗?
          • @Monodeep:您在 SocialAuth.Net 上究竟在寻找什么样的文章 - 基础知识/如何使用/如何扩展等?有一个全面的 Wiki,可以参考演示应用程序的代码来理解。
          猜你喜欢
          • 2011-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-17
          • 1970-01-01
          • 2015-01-27
          相关资源
          最近更新 更多