Component Page /BusinessClasses/FacebookConnect.cs (C#)
1: using System.Web;  
2: using System.Configuration;  
3:   
4: namespace FacebookSampleASPNETApp  
5: {  
6:     public class FacebookConnect  
7:     {  
8:         public FacebookConnect()  
9:         {  
10:         }  
11:   
12:         public bool IsConnected  
13:         {  
14:             get  
15:             {  
16:                 return (SessionKey != null && UserID > 0);  
17:             }  
18:         }  
19:   
20:   
21:         public string SessionKey  
22:         {  
23:             get  
24:             {  
25:                 string sessionKey = GetFacebookCookieValue("session_key");  
26:                 return sessionKey;  
27:             }  
28:         }  
29:   
30:         public long UserID  
31:         {  
32:             get  
33:             {  
34:                 long userID = -1;  
35:                 string uid = GetFacebookCookieValue("uid");  
36:                 if (!string.IsNullOrEmpty(uid))  
37:                 {  
38:                     uid = uid.Trim(new char[] { '"', '\\' });  
39:                     long.TryParse(uid, out userID);  
40:                 }  
41:                 return userID;  
42:             }  
43:         }  
44:   
45:         public string AccessToken  
46:         {  
47:             get  
48:             {  
49:                 //NOTE: oddly enough the accesstoken key in the cookie is actually \"access_token  
50:                 string token = GetFacebookCookieValue("\"access_token");  
51:                 return token;  
52:             }  
53:         }  
54:   
55:         private string GetFacebookCookieValue(string cookieValue)  
56:         {  
57:             string cookieName = "fbs_" + ConfigurationManager.AppSettings["AppID"];  
58:             string retString = null;  
59:             HttpCookie c = HttpContext.Current.Request.Cookies[cookieName];  
60:             if (HttpContext.Current.Request.Cookies[cookieName] != null)  
61:                 retString = HttpContext.Current.Request.Cookies[cookieName][cookieValue];  
62:   
63:             return retString;  
64:         }  
65:     }  
66:   
67: }  

.NET Documentation Tool is © 2002 - 2009 Winnersh Triangle Web Solutions Limited.