Facebook Connect 和 ASP.NET

发布于 2024-07-08 20:52:37 字数 593 浏览 9 评论 0原文

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

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

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

更新

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

I'm at step 8 of the authentication overview found here: http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works

In particular, the user has logged into facebook via Facebook Connect and their web session has been created. How do I use the facebook developer toolkit v2.0 (from clarity) to retrieve information about the user. For example, I'd like to get the user's first name and last name.

Examples in the documentation are geared towards facebook applications, which this is not.

Update

Facebook recently released the Graph API. Unless you are maintaining an application that is using Facebook Connect, you should check out the latest API: http://developers.facebook.com/docs/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏尔 2024-07-15 20:52:38

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

Facebook Connect 身份验证 for ASP.NET

我希望对某人有所帮助!

干杯,

亚当

I followed up from Bill's great article, and made this little component. It takes care of identifying and validating the user from the Facebook Connect cookies.

Facebook Connect Authentication for ASP.NET

I hope that helps somebody!

Cheers,

Adam

万人眼中万个我 2024-07-15 20:52:38

您还可以使用 SocialAuth.NET

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

You may also use SocialAuth.NET

It provides authentication, profiles and contacts with facebook, google, MSN and Yahoo with little development effort.

旧情勿念 2024-07-15 20:52:38

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

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

My two cents: a very simple project utilizing the "login with Facebook" feature - facebooklogin.codeplex.com

Not a library, but shows how it all works.

无所谓啦 2024-07-15 20:52:37

当用户使用 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;
    }
}

I had a lot of trouble figuring out how to make server side calls once a user logged in with Facebook Connect. The key is that the Facebook Connect javascript sets cookies on the client once there's a successful login. You use the values of these cookies to perform API calls on the server.

The confusing part was looking at the PHP sample they released. Their server side API automatically takes care of reading these cookie values and setting up an API object that's ready to make requests on behalf of the logged in user.

Here's an example using the Facebook Toolkit on the server after the user has logged in with Facebook Connect.

Server code:

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;
    }
}
独孤求败 2024-07-15 20:52:37

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

如何检索用户数据来自 ASP.NET 中的 Facebook Connect - Devtaulous

感谢 Calebt 在该帮助程序类上提供了良好的开端。

享受。

I followed up on this concept and wrote a full fledged article that solves this problem in ASP.NET. Please see the following.

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

Thanks to Calebt for a good start on that helper class.

Enjoy.

烟火散人牵绊 2024-07-15 20:52:37

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

此处中的必要 javascript

验证 cookie 是否与 facebook 提供的签名相匹配防止黑客攻击,请参阅了解如何开始

创建 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 Connect actually isn't too difficult, there's just a lack of documentation.

Put the necessary javascript from here

Validate the cookies match the signature provided by facebook to prevent hacking, see this for an explanation on how to get started

Create an api object (Facebook.API.FacebookAPI)
On the api object, set the application key and secret Facebook provides you when you create your app.
Set api.SessionKey and api.UserId from the cookies created for you from facebook connect.

Once that is done, you can start making calls to facebook:

Facebook.Entity.User user = api.GetUserInfo();   //will get you started with the authenticated person
書生途 2024-07-15 20:52:37

到目前为止列出的答案中缺少这一点:

登录成功后,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 文件。

This is missing from the answers listed so far:

After login is successful, Facebook recommends that you validate the cookies are in fact legit and placed on the client machine by them.

Here is two methods that can be used together to solve this. You might want to add the IsValidFacebookSignature method to calebt's Utility class. Notice I have changed his GetFacebookCookie method slightly as well.

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;
    }

The SecretKey and ApiKey are values provided to you by Facebook. In this case these values need to be set, preferably coming from the .config file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文