Category Archives: Facebook

All topics related to Facebook posting, as well as writing API and call backs to Facebook to fetch real time Facebook data.

HOW TO: Use Facebook Registration In .NET

As we all know Facebook is too big of a monster to not consider when developing our web applications.  Time and time again I have received the request to add a Facebook registration process to a store or blog that allows users to “Connect With Facebook” and remember their settings.  Luckily, Facebook has begun alpha support of a C# library which lets us have some fun with JSON.  Facebook uses a signed response method which encrypts information passed back and forth using your application secret and ID.  Here is some sample code for a page that would call to the Facebook API for registration/logging in.

HTML

<iframe src="https://www.facebook.com/plugins/registration.php?
client_id=xxxx&
redirect_uri=http://localhost/fbtest/completed.aspx&
fields=name,first_name,last_name,birthday,gender,location"
scrolling="auto"
frameborder="no"
style="border:none"
allowTransparency="true"
width="100%"
height="330">
</iframe>

When using this approach Facebook will pass you back to the page you specify in the redirect_url with a Form field setup called signed_request.  The nice thing about this is you create a page that accepts this call back and handles the Form field appropriate to login or register a user.  The C# library that Facebook has started working on exposes some methods which allow you to interpret this response and get some data. Here is an example on how to retrieve the response and fetch some fields from it:

VB.NET

Imports Newtonsoft.Json.Linq
'...
Partial Class FacebookRegistrationPageHandler
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim fbaFacebookApp As New Facebook.FacebookApp
        Dim fsrSignedRequest As Facebook.FacebookSignedRequest
        Dim jobJSONObject As JObject

        fbaFacebookApp.AppId = "xxxx"
        fbaFacebookApp.AppSecret = "xxxx"
        fsrSignedRequest = fbaFacebookApp.SignedRequest

        jobJSONObject = JObject.Parse(fsrSignedRequest.Dictionary("registration"))
        Dim strReturnedName = CType(jobJSONObject("name"), Newtonsoft.Json.Linq.JValue).Value

    End Sub
End Class

C#

using Newtonsoft.Json.Linq;
// ...
Partial;
class FacebookRegistrationPageHandler : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        Facebook.FacebookApp fbaFacebookApp = new Facebook.FacebookApp();
        Facebook.FacebookSignedRequest fsrSignedRequest;
        JObject jobJSONObject;
        fbaFacebookApp.AppId = "xxxx";
        fbaFacebookApp.AppSecret = "xxxx";
        fsrSignedRequest = fbaFacebookApp.SignedRequest;
        jobJSONObject = JObject.Parse(fsrSignedRequest.Dictionary("registration"));
        object strReturnedName = ((Newtonsoft.Json.Linq.JValue)(jobJSONObject["name"])).Value;
    }
}