[英]Facebook Connect and ASP.NET
我在此處的身份驗證概述的第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/
一旦用戶使用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;
}
}
我跟進了這個概念並編寫了一篇完整的文章,解決了ASP.NET中的這個問題。 請參閱以下內容。
如何從ASP.NET中的Facebook Connect中檢索用戶數據 - 妄想
感謝Calebt在幫助程序類上的良好開端。
請享用。
Facebook Connect實際上並不太難,只缺少文檔。
從這里輸入必要的javascript: http : //tinyurl.com/5527og
驗證cookie與facebook提供的簽名相匹配以防止黑客入侵,請參閱: http : //tinyurl.com/57ry3s獲取有關如何入門的說明
創建一個api對象(Facebook.API.FacebookAPI)在api對象上,設置應用程序密鑰和Facebook在您創建應用程序時提供的秘密。 從facebook connect為您創建的cookie設置api.SessionKey
和api.UserId
。
完成后,您可以開始撥打facebook:
Facebook.Entity.User user = api.GetUserInfo(); //will get you started with the authenticated person
到目前為止列出的答案中缺少這個:
登錄成功后,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文件。
我跟蹤了比爾的偉大文章,並制作了這個小組件。 它負責從Facebook Connect cookie中識別和驗證用戶。
用於ASP.NET的Facebook Connect身份驗證
我希望能幫到別人!
干杯,
亞當
您也可以使用SocialAuth.NET
它提供身份驗證,配置文件和與Facebook,谷歌,MSN和雅虎的聯系,只需很少的開發工作。
我的兩分錢:一個非常簡單的項目,利用“登錄Facebook”功能 - facebooklogin.codeplex.com
不是圖書館,而是展示它是如何運作的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.