cka*_*ass 45 asp.net facebook facebooktoolkit
我在此处的身份验证概述的第8步:http://wiki.developers.facebook.com/index.php/How_Connect_Authentication_Works
特别是,用户已经通过Facebook Connect登录Facebook并且已经创建了他们的网络会话.如何使用facebook developer toolkit v2.0(从清晰度)检索有关用户的信息.例如,我想获取用户的名字和姓氏.
文档中的示例适用于Facebook应用程序,但事实并非如此.
Facebook最近发布了Graph API.除非您要维护使用Facebook Connect的应用程序,否则您应该查看最新的API:http: //developers.facebook.com/docs/
小智 23
一旦用户使用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
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 15
我跟进了这个概念并编写了一篇完整的文章,解决了ASP.NET中的这个问题.请参阅以下内容.
如何从ASP.NET中的Facebook Connect中检索用户数据 - 妄想
感谢Calebt在帮助程序类上的良好开端.
请享用.
cka*_*ass 13
Facebook Connect实际上并不太难,只缺少文档.
从这里输入必要的javascript:http://tinyurl.com/5527og
验证cookie与facebook提供的签名相匹配以防止黑客攻击,请参阅:http://tinyurl.com/57ry3s获取有关如何入门的说明
创建一个api对象(Facebook.API.FacebookAPI)在api对象上,设置应用程序密钥和Facebook在您创建应用程序时提供的秘密.设置api.SessionKey和api.UserId从facebook连接创建的cookie.
完成后,您可以开始拨打facebook:
Facebook.Entity.User user = api.GetUserInfo(); //will get you started with the authenticated person
Run Code Online (Sandbox Code Playgroud)
到目前为止列出的答案中缺少这个:
登录成功后,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;
}
Run Code Online (Sandbox Code Playgroud)
SecretKey和ApiKey是Facebook提供给您的值.在这种情况下,需要设置这些值,最好来自.config文件.
我跟踪了比尔的伟大文章,并制作了这个小组件.它负责从Facebook Connect cookie中识别和验证用户.
用于ASP.NET的Facebook Connect身份验证
我希望能帮到别人!
干杯,
亚当
| 归档时间: |
|
| 查看次数: |
61892 次 |
| 最近记录: |