Ok, the long and the short of it is that developing Facebook applications using the .NET Framework is an absolute pain in the backside. The API is counter-intuitive and the documentation is poor. I've therefore decided that as and when I find helpful ways of doing things that are otherwise difficult, I will post them here in the hope that they help someone!
My first tip is basic application setup once you have acquired the Facebook dev pack. For all purposes here I am using IFrame dev, not FBML. The FBML setup simply caused too many problems for me without any substantial benefit.
So, my first Facebook.NET dev tip is, set up a master page and declare the following:
public facebook.Components.FacebookService fbService = new facebook.Components.FacebookService();
public bool testMode = true;
private const string FACEBOOK_API_KEY = "your_api_key_here";
private const string FACEBOOK_SECRET = "your_secret_key_here";
Then, the following method:
protected void Page_Init(object sender, EventArgs e)
{
fbService.ApplicationKey = FACEBOOK_API_KEY;
fbService.Secret = FACEBOOK_SECRET;
fbService.IsDesktopApplication = false;
string sessionKey = (string)Session["facebook_session_key"];
long userId = 0;
string authToken = Request.QueryString["auth_token"];
if (!String.IsNullOrEmpty(sessionKey))
{
userId = (long)Session["facebook_userId"];
fbService.SessionKey = sessionKey;
fbService.uid = userId;
}
else if (!String.IsNullOrEmpty(authToken))
{
try
{
fbService.CreateSession(authToken);
}
catch (Exception)
{
Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0", true);
}
Session["facebook_session_key"] = fbService.SessionKey;
Session["facebook_userId"] = fbService.uid;
Session["facebook_session_expires"] = fbService.SessionExpires;
}
else
{
Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0", true);
}
if (!IsPostBack)
{
}
else
{
}
}
Note that this code goes in the master page's Page_Init because this fires before the sub-page's Page_Load event.
Next up, inside the sub-page you want to change the declarations at the top of the .aspx (assuming you are using code-behind) to read as follows:
<%@ Page [SNIP="~/mymaster.Master" %>
<%@ MasterType VirtualPath="~/mymaster.Master"%>
Note that [SNIP] is just a placeholder for other tags which should go there - don't actually put that in.
This now means that inside your code-behind you will be able to access the Master.fbservice object which, as I shall show in the next snippet will enable us to start actually building something useful.