my6solutions

asp .net, the social web & other distractions

 

Running Apps


PayPal - The safer, easier way to pay online!

Disclaimer

I am in no way affiliated with Microsoft or Google. I am just another developer trying to make a difference. All opinions and observations are usually my own.

Integration of Facebook Connect on ASP .NET MVC, Part 1

Update 2: The changes to the Facebook Toolkit library are now available for download from the Facebook Toolkit project page

Update: I've just updated raya with the Facebook Connect changes. There were some initial problems with "invalid session key" issues but that has been resolved.

Over the weekend, I had look into integrating Facebook Connect into raya. The example given on the facebook developer wiki on how to do this is based on an example in PHP. The first lazy thing I did was to search some more and see if there were any .NET examples. Nope.

However, I found two .NET client libraries that would hopefully make my life easier; Facebook.NET and Facebook Developer Toolkit. At first glance, Facebook.NET seems targeted towards the use of facebook controls. Meanwhile, the FBT provides a more code intensive implementation. Some people seem to have gripes about the FBT. It seems to me that the FBT is closer to the facebook PHP client library. I'm guessing that if you used the FBTand followed any facebook PHP example, your ASP .NET implementation will be quite similar.

The question now is, what about ASP .NET MVC?  No server-side controls on ASP .NET MVC ... how now brown cow?

Looking at the PHP example for Facebook Connect, I have managed to implement it using FDT. However, there were some minor additions that I had to add to the FDT to make this happen.I will attempt to explain how I did this in the following paragraphs. I will upload the code changes to support Facebook Connect onto raya. This will be Part 1 of an xx Part series as I don't know how many suitable size chunks I should split it to. Now on with the story ...

1. First, you will need add the facebook project under 'Source\Facebook' to your current solution. Normally, you just need to reference the DLL but we need to make some changes.

2. Reference the file Microsoft.Xml.Schema.Linq.dll from the 'Source\Facebook\bin\Release' folder. This is to get the cool LINQness from parsing an XML string using LINQ to XSD. Scott Hanselman has an article on this. Did I mention that it's an alpha release too?

3. Add the schema, facebook.xsd, from 'Source\Facebook\Schema' to 'C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas'. This is to get Visual Studio to recognise the FBML tag.

4. You will need to update your MasterPage's or ViewPage's <html> tag to get VS to recognise FBML tags

    5 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://api.facebook.com/1.0/">

 

5. You will also need to reference the following script files

    3 <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" 

    4     type="text/javascript"></script>

    5     <script src="/Scripts/fb/fbconnect.js" type="text/javascript"></script>

 

6. To display the login button, insert the following 

   27 <fb:login-button length="long" background="light" size="medium"

   28                 onlogin="facebook_onlogin_ready();"></fb:login-button>

 

7. In the same page, add the following javascript and replace APIKEY with your facebook api key that is given for the facebook application that you registered. You will need to register a facebook application to use Facebook Connect to get the API key. You will also need to set the Connect URL. Point this url to your site address. Using a localhost address is ok when debugging locally.

   33 <script type="text/javascript">

   34     FB_RequireFeatures(["XFBML"], function() {

   35         FB.Facebook.init(

   36     "<%= APIKEY %>", "/Content/xd_receiver.htm"); });

   37 </script>

 

8. You will need the following additional files, which I have obtained from the PHP example of Facebook Connect.

xd_receiver.htm (322.00 bytes)

fbconnect.js (3.87 kb) 

9. The changes to the FDT are also in the following files:

API.cs (24.23 kb)

connect.cs (3.06 kb)

users.cs (8.06 kb)

10. This will allow your site to show the Facebook connect button that will display a pop-up allowing your site users to log into their facebook account. From here, your site will be able to obtain the user's facebook session_key cookie. 

Bookmark and Share

Categories: ASP .NET | facebook
Permalink | Comments (10) | Post RSSRSS comment feed

Comments

Marat

Wednesday, April 29, 2009 3:03 PM

Marat

Sean,

thanks for the post. I'm having the following issue and I hope you can help. I'm implementing a facebook connect functionality on my site. If I'm not logged in to Facebook and I click on the facebook connect login button it brings up the facebook prompt requesting email and password to login. That works fine.  However, If i'm already logged into Facebook and I click on the facebook connect login button, it flashes the prompt for less than a second and then it disappears. What am I doing wrong?  I've seen this on other sites and it works fine, it opens up the login prompt already showing my profile picture and without the email and password fields. Thanks for your help.

Sean

Wednesday, April 29, 2009 5:46 PM

Sean

Hmm, have you checked that the Connect URL is set to the URL of the page users are forwarded to when they are login? Or do you have it online so I can have a look?

Chanan

Sunday, May 17, 2009 3:48 AM

Chanan

Very interesting post!
I'm trying to use Facebook Connect on a ASP.NET MVC project.
fro some reason I don't get any cookie or session saved on localhost after login. do you have any idea what am I doing wrong?

Admin

Sunday, May 17, 2009 1:40 PM

Admin

If you have not done so, try using firebug to check for errors in the response. Do you have it online so I could have a look?

Andrew

Tuesday, July 07, 2009 8:06 PM

Andrew

Hi,

I was wondering if you could tell me / explain if this is possible:
A user logs into my site by clicking the Facebook Connect login button. He enters his info and is logged into FB. At this point, can my site check if the logged in FB User already has an account on my site and if he does, link his FB account to it? I've seen this done on Digg and Vimeo, where after you log in via FB Connect, they check if you already have an account on their site.

Portman

Thursday, July 30, 2009 3:23 AM

Portman

I'm trying to get raya working w/ FB Connect under localhost developmetn on Cassini.

The <fb:login-button> doesn't render. I'm guessing its because I don't have the correct "Connect URL". Currently I've tried

http://localhost:{port}/
http://localhost:{port}/home
http://localhost:{port}/Content

What's the correct Connect URL to use in the Facebook application settings to get this working under local development?

Thanks.

Portman

Thursday, July 30, 2009 3:33 AM

Portman

Nevermind previous.  This connect URL works:

http://localhost:{port}/


The issue was with some unrelated Presta.NET code throwing a JavaScript error which meant the FB Connect JS never executed. Working now. Thanks for the great sample.

Admin

Thursday, July 30, 2009 5:39 AM

Admin

Have a look here, wiki.developers.facebook.com/.../Anatomy_of_a_Facebook_Connect_Site

It pretty much sums up everything you need to understand about Facebook Connect

Admin

Thursday, July 30, 2009 5:40 AM

Admin

static.ak.connect.facebook.com/.../...ureLoader.js is required to render the XFBML tags

Hezi

Thursday, September 03, 2009 8:43 PM

Hezi

Hello,

I am trying to integrate my ASP.Net MVC site with Facebook Connect.

For some odd reason after the login popup loads and connets, HttpContxt.Current.Requet.Cookies[Session_Key_Cookie_Name] still returns NULL!

What could be the possible reason?

Thanks

P.S

I also noticed that although I've set localhost/xd_receiver in the Site.Master FB.init call, xd_receiverController.cs doesn't get called (Set a breakpoint in it)

( I followed all those posts/articles when buiding my site: devtacular.com/.../ , stackoverflow.com/.../facebook-connect-and-asp-net, facebooktoolkit.codeplex.com/.../View.aspx my6solutions.com/.../...nnect-on-ASP-NET-MVC.aspx)