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

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

问题描述

我在身份验证概述第8步在这里找到:<一href=\"http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works\">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连接登录到Facebook和他们的网络会话已创建。我如何使用Facebook的开发工具包2.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最近发布的图形API。除非你坚持认为是使用Facebook Connect的应用程序,你应该检查出最新的API: http://developers.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连接。关键是,Facebook的群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工具包一个例子,用户登录时后Facebook连接。

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

服务器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

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连接和ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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