Writing Facebook Apps in .NET Part 1

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)
        {
            // ApplicationKey and Secret are acquired when you sign up for 
            fbService.ApplicationKey = FACEBOOK_API_KEY;
            fbService.Secret = FACEBOOK_SECRET;
            fbService.IsDesktopApplication = false;

            string sessionKey = (string)Session["facebook_session_key"];
            long userId = 0;

            // When the user uses the facebook login page, the redirect back here will will have the auth_token in the query params
            string authToken = Request.QueryString["auth_token"];

            // We have already established a session on behalf of this user
            if (!String.IsNullOrEmpty(sessionKey))
            {
                userId = (long)Session["facebook_userId"];
                fbService.SessionKey = sessionKey;
                fbService.uid = userId;
            }
            // This will be executed when facebook login redirects to our page
            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;

            }
            // Need to login
            else
            {
                Response.Redirect(@"http://www.facebook.com/login.php?api_key=" + fbService.ApplicationKey + @"&v=1.0", true);
            }

            if (!IsPostBack)
            {
		// do what you want here for when it is not a postback

            }
            else
            {
		// do what you want here on every postback
            }
        }

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.

comment from vin
Hi, Is there any way to pass client culture with "http://www.facebook.com/login.php?api_key...." to facebook login page so that it renders the login page in client culture? Or any other method so when redirecting to URL http://www.facebook.com/login.php?api_key... will detect client culture and render the loginin page in client's culture. Thanks

add a comment
name:
website:
email:
comment: