public class Facebook
{
public readonly API api;
public Facebook()
{
api = new API
{
ApplicationKey = ApiKey,
SessionKey = SessionKey,
Secret = SecretKey,
uid = UserID
};
}
public bool isConnected()
{
if (api.SessionKey == null)
{
return false;
}
// can't really tell if the session key we have is still valid
// let's try connecting
try
{
api.uid = api.users.getLoggedInUser();
}
catch (FacebookException)
{
// invalid session key found
ClearFacebookCookies();
return false;
}
return true;
}
public static string ApiKey
{
get
{
return RayaSettings.Fb_api_key;
}
}
public static string SessionKey
{
get
{
return GetFacebookCookie("session_key");
}
}
public static string SecretKey
{
get
{
return RayaSettings.Fb_api_secret;
}
}
public static long UserID
{
get
{
int userID;
int.TryParse(GetFacebookCookie("user"), out userID);
return userID;
}
}
private static string GetFacebookCookie(string cookieName)
{
string retString = null;
string fullCookie = ApiKey + "_" + cookieName;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Request.Cookies[fullCookie] != null)
retString = HttpContext.Current.Request.Cookies[fullCookie].Value;
}
return retString;
}
public static void ClearFacebookCookies()
{
string[] cookies = new[]{"user", "session_key", "expires", "ss"};
foreach (var c in cookies)
{
string fullCookie = ApiKey + "_" + c;
if (HttpContext.Current != null &&
HttpContext.Current.Response.Cookies[fullCookie] != null)
{
HttpContext.Current.Response.Cookies[fullCookie].Expires = DateTime.Now.AddMonths(-1);
}
}
}
}