Home Blog .NET Facebook authentication using FacebookSDK for .NET
Facebook authentication using FacebookSDK for .NET
Written by Dicky   
Friday, 20 May 2011 10:35
AddThis Social Bookmark Button

There are many attempts to create Facebook SDK for .NET. Up until recently, none of it is considered successful due to ever expanding and changing Facebook API and the use of strongly-typed library which make it hard to maintain. Since the introduction of .NET 4.0 and dynamic datatype, there is another attempt to create a .NET Facebook library and to be honest, this one looks promising. The library is called Facebook C# SDK. After browsing around the website which lacks of example. I put a together simple page on how to do Authentication using the SDK.

Here's what you put in .aspx page:

<form id="form1" runat="server">
    <div id="fb-root"></div>
    <fb:login-button autologoutlink="true" perms="email" onlogin="window.location.reload();"></fb:login-button>
<asp:Label ID="lblName" runat="server"></asp:Label>
    <script type="text/javascript">
            window.fbAsyncInit = function() {
                FB.init({ appId: 'YOUR_AAP_ID', status: true, cookie: true, xfbml: true });

                /* All the events registered */
                FB.Event.subscribe('auth.login', function(response) {
                    // do something with response
                FB.Event.subscribe('auth.logout', function(response) {
                    // do something with response
            (function() {
                var e = document.createElement('script');
                e.type = 'text/javascript';
                e.src = document.location.protocol +
                e.async = true;

And here's the code-behind:

    protected void Page_Load(object sender, EventArgs e)
        if (Request.Cookies["fbs_YOUR_APP_ID"].Value == null)

        string fbsValue = Request.Cookies["fbs_YOUR_APP_ID"].Value;
        Dictionary<string, string> tokens = new Dictionary<string, string>();
        fbsValue = fbsValue.Substring(1, fbsValue.Length - 2);

        foreach (string token in fbsValue.Split('&'))
            tokens.Add(token.Substring(0, token.IndexOf("=")),
                token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
        Session["accessToken"] = tokens["access_token"];

        var client = new FacebookClient(Session["accessToken"].ToString());
        dynamic profile = client.Get("me");
        lblName.Text = profile["first_name"];

You can make your code cleaner by putting "YOUR_APP_ID" in web.config but you got the basic idea. From here you can simply create a new FacebookClient object and passed in the access_token each time. With this new SDK, you basically can request whatever you want with similar method as the PHP Facebook SDK using the Get() method. So instead of using $facebook->api('me/friends'); to get friends' list in PHP, you simply use client.Get("me/friends"); in .NET or to get photo albums, you need "/me/albums". I guess that's all for now :)