Thursday, April 29, 2010

Facebook IFrame Application With ASP.NET and MVC

There are several ways of developing a Facebook (fb) application.  Prior to deciding on your implementation method, make sure you read this article that explains the difference betwen an FBML and an IFrame application.  This particular example deals with an IFrame application developed in ASP.NET (4.0 Framework) and MVC 2.  So let's get going...

1.  Download the Facebook SDK.

2.  Start a new MVC web application project in Visual Studio and add your references for:
  • Facebook.dll
  • Facebook.Web.dll
  • Facebook.Web.Mvc.dll
3.  Create/register your fb application.

4.  Essential fb application information:

API Key and Secret are very important, because that's how your MVC app will identify itself to fb, and thus your fb session will be tracked using those two values as HTTP requests jump from Facebook to your application and back.  I recommend saving your API Key and Secret to the web.config.


5.  Configure fb application:

Make sure to set your Canvas Page URL, Canvas Callback URL and Render Method (IFrame).
The Canvas Callback URL is very important - it's the root path to your application that is hosted elsewhere.

While in Sandbox mode (explained below), you can temporarily change your Canvas Callback URL to http://localhost/kons_first_app/ - that should help you debug the application and step into your code.

Sandbox Mode:
While your application is under construction, be sure to enable Sandbox mode, to prevent other people on Facebook, with the exception of other developers (you can add devs in the Basic tab), from playing with or even seeing your half-done application.

6.  Using the fb API (provided by the SDK):


As you can see, we first have to get a handle to the fb Api.  Once we do that, the fb world is our oyster!  The above code is accessing the current user's profile user properties.

Notice the FacebookAuthorization attribute above our Action method.  Among other things, this handles the fb session authentication for you, so that you don't have to pass apiKey and secret into the GetApi() method.  Instead, they'll be retrieved automatically from the appSettings in your web.config (so don't forget to put them in the web.config!).

You might also be wondering how GetApi() is available via this instance, when we're in a class inheriting from Controller.

Fb SDK to the rescue!
Facebook.Web.Mvc provides us with a ControllerExtension class that implements GetApi(), thus saving us the headaches of.... writing more code.

However..  You'll quickly realize that to do any real/robust development and interaction with fb, this ain't gonna cut it.  For example, one difficulty I ran into was routing to a different/custom Action with the FacebookAuthorization attribute in place and IsFbml set to false (as it should always be for an IFrame app).  All requests kept getting routed to Index (I have a feeling fb session management was getting screwed up with the requests jumping from fb to my app).  However, I wanted to leverage automatic fb authentication and was dreading having to pass the apiKey and secret in every Action method.

So here's a more elegant solution... write a base fb controller class (which of course inherits from Controller) that handles all fb authentication and session management for you. Here are some minimal things you'll want to do in your base fb controller:

First things first, get a hold of your fb session:

Now that you can access your fb session, create an accessor for the Facebook.Rest.Api:


Almost forgot the constructor!  In the constructor, you'll want to at the very least set your API Key and Secret (from the web.config):


And that's it.  You can now create controllers that inherit from your base fb controller and make simple calls like this (without having to authenticate to fb every time):

7-ish.  Client-side fb stuff
I won't cover it here, but would highly recommend some light reading on XFBML and FBJS to learn how you can leverage fb UI code within your IFrame app.

Tuesday, April 27, 2010

MVC 2 blocks JSON GET requests by default

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

Welcome to MVC 2!
For security reasons, MVC 2 blocks JSON GET requests by default.  So if you really want to call that JsonResult method via a GET, you must explicitly allow it like this:
 

Friday, April 16, 2010

Replacing Notepad with Flo's Notepad2 on Windows 7

1.  Once you downloaded Flo's Notepad2, get the Auto Install Script (link available here).

Auto Install Script (batch file) should look something like this:

@echo off
TITLE Notepad2 Install Script for Complete Vista Notepad Replacement
echo.
echo Notepad2 Install Script for Complete Vista Notepad Replacement
echo Version 1.0
echo.
echo (c) My Digital Life (www.mydigitallife.info)
echo.
echo.
echo.
echo.echo Confirm to apply? (Press Ctrl-C and answer Y to terminate)
pause
echo.
echo.

takeown /f %Systemroot%\notepad.exe
takeown /f %Systemroot%\System32\notepad.exe
icacls %Systemroot%\notepad.exe /grant "%username%":f
icacls %Systemroot%\System32\notepad.exe /grant "%username%":f
IF EXIST %SYSTEMROOT%\SysWOW64 (bcdedit.exe -set loadoptions "DDISABLE_INTEGRITY_CHECKS")
copy %Systemroot%\notepad.exe %Systemroot%\notepad.original.exe
copy %Systemroot%\System32\notepad.exe %Systemroot%\System32\notepad.original.exe 
echo.
echo Original notepad.exe has been renamed to "notepad.original.exe" in its original folder.
echo.
copy notepad2.exe %Systemroot%\notepad.exe /y
copy notepad2.exe %systemroot%\System32\notepad.exe /y
echo.
echo Notepad2 installation is completed.
echo If no error occurred, Notepad2 will now replace all Notepad functions.
echo.
pause

2.  Copy the Notepad2.exe that you downloaded into the following folders:
  • C:\Windows\ 
  • C:\Windows\System32\
3.  Take ownership of the original notepad.exe in the following folders:
  • C:\Windows\ 
  • C:\Windows\System32\ 
4.  Then run the Auto Install Script.

Run notepad and you should be good to go.

Vault's file diff not working on Windows 7

This applies to Vault client v3.1.7 running on Windows 7.

If you get an error, saying "'Diff application /sgdm.exe could not be found" then first, try to download and install the latest Vault client.  If that's not an option for you, then try this:

In Vault, go to Tools -> Options -> Diff/Merge:


And replace all instances of  %VAULT_EXE_DIR%  with an absolute path to sgdm.exe.

 More info can be found here:  http://support.sourcegear.com/viewtopic.php?f=5&t=12841