Category Archives: Linq

Topics related to using LINQ for object access and manipulation.

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;
    }
}

HOW TO: Query Objects in LINQ Using VB.NET And C#

LINQ is a powerful component that Microsoft introduced into the .NET framework a few years ago. Since then, the syntax and ideology has been adopted by Java, PHP and Javascript. The concept behind it is adding database like querying into your code allowing you to select objects from collections in a tuned manner rather than iterate through collections and filtering things out. Let us say we have a simple SQL statement below which selects customers from a customer table based on their state:

SELECT Name,Address FROM Customer
WHERE State = ‘CA’

If you had the same data in your application represented by objects, you could do the same kind of query using LINQ. Here is some sample syntax for selecting objects from a collection based:

VB.NET

Dim q = From c In Customers
                  Where c.State = "CA"
                Select c.Name, c.Address

C#

IEnumerable q = from c in Customers
                               where c.State = "CA"
                               select c.Name, c.Address;

The performance scales very well, and I have almost replaced iterating at this point. If you are interested, click here for information on LINQ.