Facebook Connect和ASP.NET [英] Facebook Connect and ASP.NET

查看:105
本文介绍了Facebook Connect和ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到认证概述的第8步: http:// wiki .developers.facebook.com / index.php / How_Connect_Authentication_Works

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

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

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.

文档中的示例适用于Facebook应用程序,这不是。

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

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

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/

推荐答案

我有很多麻烦找出如何使服务器端调用一次一个用户使用Facebook Connect登录的用户。关键是Facebook Connect JavaScript在客户端成功登录后设置Cookie。您可以使用这些cookie的值在服务器上执行API调用。

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.

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

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.

用户使用Facebook Connect登录后,服务器上的 Facebook Toolkit

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

服务器代码:

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

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

这篇关于Facebook Connect和ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆