Trouble getting started

Mar 24, 2010 at 5:03 AM
Edited Mar 24, 2010 at 5:10 AM

Hi there,

I'm trying to create an MVC app and here are my issues...

1) Does validate the sessions? I can't find any sign of the cookie values being hashed in the source code...

2) Why does the Controller extension method 'GetApi' create a 'FBMLCanvasSession' and not a 'ConnectSession'?

3) That's the easy stuff... When I get the API, it doesn't appear to load the data from the cookies. Put it this way, I can retrieve user info for a hard coded user id like so...

            var fbApi = this.GetApi(ConfigurationManager.AppSettings["FacebookConnectApiKey"], ConfigurationManager.AppSettings["FacebookConnectAppSecret"]);

          var user1= fbApi.Users.GetInfo(1234567);

...but when I do this...


is says...

A session key is required for calling this method

Any ideas? I'd have thought that the API would load the session info in when first created or something.

Cheers, Ian.

Apr 4, 2010 at 5:33 AM

I faced a similar issue. Here is my understanding after playing around with FB APIs and MVC for a bit -  

#1. Yes, it does on the first call which is typically to /Home/Index. FB page passes in a bunch of query string params to the IFrame app that are stored by FDT in the FBMLCanvasSession object. Users.GetLoggedInUser() will work in this Action.

#2. No idea.

#3. You are probably seeing this behavior when navigating to another Controller/Action. This is because with each server side call to your app, FDT will recreate the FBMLCanvasSession object. Only this time it does not have the query string params that FB had passed in to /Home/Index. So it is essentially an empty session that almost has no permissions (I am still figuring out the exact differences between the first and the second session). So the call fails.

I think there are a couple of ways to get around this. One is to remember to include the initial query string params to all Controller/Actions. I found this too cumbersome to use. The second way to get around this is to store the FBMLCanvasSession in a ASP Session object in /Home/Index and then retrieve it in subsequent /Controller/Actions. However, there is no way to "set" the session, one can only "get" it. To get around this I added an override to the GetApi extension that will take in a cached session and everything works fine. Note, this may have some unintended side-effects that I am not aware of yet so it might be completely the wrong thing to do. But it has unblocked me so far :-)

Here is what the Controller code looks like - 

public ActionResult Index() {
            Api api = this.GetApi();
            //long userId = api.Users.GetLoggedInUser();            
            long userId = api.Session.UserId;
            Session["userId"] = userId;
            Session["fbSession"] = api.Session;
            ViewData["debugMsg"] = api.Session.RequiredPermissions;
            ViewData["userId"] = userId;            
            return View();            
public ActionResult Index() {
    Api api = this.GetApi();
    Session["fbSession"] = api.Session;                     
    return View();            

public ActionResult Friends() {
    FBMLCanvasSession cachedSession = (FBMLCanvasSession)Session["fbSession"];
    Api api = this.GetApi(cachedSession);
    long userId = api.Users.GetLoggedInUser();


Here is the extension method added to ControllerExtensions.cs in the Facebook.Web.Mvc project. I could've added this anywhere but this is something that all FB MVC projects would use so it makes sense to add it to FDT itself - 


public static Api GetApi(this Controller controller, FBMLCanvasSession cachedSession) {
    return new Api(cachedSession);






Aug 6, 2010 at 8:25 PM
What if this error shows up only in tab mode without redirecting to different actions/controllers? I have my mvc app working fine in full page view but it fails when viewed as tab. Regards.