Question

I have an IFrame Facebook application that uses Facebook C# SDK. I also use jquery and load some page components asynchronously. For example, I call the following javascript from main page(at this point user is authenticated)

$.ajax({            
    type: "POST",
    url: "Ajax/GetMyBalance.aspx",        
    datatype: 'text',
    success: function (html) {
        $('#balance_div').html(html);
    }
    });

The problem is that in GetMyBalance.aspx:PageLoad the user is not authenticated. I tried adding

 FacebookApp app = new FacebookApp();
 CanvasAuthorizer auth = new CanvasAuthorizer(app);
 if (!auth.IsAuthorized()) // always true when page is loaded first time
 {
    var url = auth.GetLoginUrl(new HttpRequestWrapper(Request));
    var content = CanvasUrlBuilder.GetCanvasRedirectHtml(url);
    Response.ContentType = "text/html";
    Response.Write(content);
    Response.End();
 }

to GetMyBalance.aspx:PageLoad ; it did authenticate the user, but it also redirected browser to Myapp/Ajax/GetMyBalance.aspx which might make sense, but it's absolutely not what I wanted. How can I authenticate user in this case ? Thanks for your answers.

Was it helpful?

Solution

You can do it with no cookies. Set cookieSupport = false in the SDK settings and initialize the Javascript client library with cookie=false. Then get the auth token using the Javascript client library before you post, pass the auth token up to the server, and use it to instantiate your FacebookApp instance.

        FB.getLoginStatus(function (response) {
          if (response.session) {
            $('#AuthToken').val(response.session.access_token);
            $('form').ajaxSubmit(options);
            return false;
          } else {
            // not logged in, prompt them to retry
            return false;
          }
        }, true);

OTHER TIPS

Have you tried enabling cookie support in facebookSettings?

<facebookSettings apiKey="blah" apiSecret="blah" appId="blah" cookieSupport="true" />

Well, cookieSupport does not seem to be a valid property:

"Parser Error Message: Unrecognized attribute 'cookieSupport'. Note that attribute names are case-sensitive."

--- update ---

My apologies, I found this: http://facebooksdk.codeplex.com/releases/view/59012

At SDK 4.2.1 cookieSupport is automatic, that is why I got the error

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top