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

查看:23
本文介绍了Facebook 连接和 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 developer toolkit 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://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 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 工具包的示例.

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

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天全站免登陆