Facebook Connect和ASP.NET

jjhzyzn0  于 2023-04-22  发布在  .NET
关注(0)|答案(7)|浏览(169)

我现在处于身份验证概述的第8步:http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works
特别是,用户已经通过Facebook Connect登录到facebook,并且他们的Web会话已经创建。如何使用facebook开发人员工具包v2.0(来自clarity)来检索有关用户的信息。例如,我想获取用户的名字和姓氏。
文档中的示例是针对facebook应用程序的,而这不是。

更新

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

avkwfej4

avkwfej41#

当用户登录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
}

Utility.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;
    }
}
wlwcrazw

wlwcrazw2#

我跟进了这个概念,并写了一篇完整的文章来解决ASP.NET中的这个问题。
How to Retrieve User Data from Facebook Connect in ASP.NET - Devtacular
感谢Calebt为帮助类提供了一个良好的开端。
好好享受

8hhllhi2

8hhllhi23#

Facebook Connect实际上并不太难,只是缺乏文档。
here中的必要javascript
验证cookie是否与Facebook提供的签名匹配以防止黑客攻击,请参阅this了解如何开始
创建一个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
w3nuxt5m

w3nuxt5m4#

这是迄今为止列出的答案中缺少的:
登录成功后,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文件。

yzuktlbb

yzuktlbb5#

我从比尔的伟大的文章跟进,并作出了这个小组件.它负责识别和验证用户从Facebook连接cookie.
Facebook Connect Authentication for ASP.NET
我希望这对某人有帮助!
干杯
亚当

nzrxty8p

nzrxty8p6#

您也可以使用SocialAuth.NET
它提供身份验证,配置文件和与Facebook,谷歌,MSN和雅虎的联系人,几乎没有开发工作。

neekobn8

neekobn87#

我的两分钱:一个非常简单的项目,利用“登录Facebook”功能-facebooklogin.codeplex.com
不是图书馆,但展示了这一切是如何工作的。

相关问题