Question

I'm new to Facebook apps. I'm trying to create an MVC 4 application with Facebook Application as my Project Template. I'm trying to catch the page id on which the page tab is created and I've got it somehow. My problem here is when someone visits my app, I want to know the page id through which they are viewing the page tab. I've searched a lot where I got to know that I've to use FacebookSignedRequest for this. But this class is not available to me.

Thanks in advance for any help.

Was it helpful?

Solution 2

All I had to do was create a Facebook Client object and call the ParseSignedRequest method with the app secret.

var fb = new FacebookClient();
dynamic signedRequest = fb.ParseSignedRequest(appSecret, Request.Form["signed_request"]);

This returns a Json object which we have to parse using JObject.Parse

OTHER TIPS

If you are simply trying to parse the signed_request parameter from Facebook, you can do so using the following C# code.

This code also verifies the hash using your own app_secret param, to ensure the signed_request originated from Facebook.

public static string DecodeSignedRequest(string signed_request)
{
    try
    {
        if (signed_request.Contains("."))
        {
            string[] split = signed_request.Split('.');

            string signatureRaw = FixBase64String(split[0]);
            string dataRaw = FixBase64String(split[1]);

            // the decoded signature
            byte[] signature = Convert.FromBase64String(signatureRaw);

            byte[] dataBuffer = Convert.FromBase64String(dataRaw);

            // JSON object
            string data = Encoding.UTF8.GetString(dataBuffer);

            byte[] appSecretBytes = Encoding.UTF8.GetBytes(app_secret);
            System.Security.Cryptography.HMAC hmac = new System.Security.Cryptography.HMACSHA256(appSecretBytes);
            byte[] expectedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(split[1]));
            if (expectedHash.SequenceEqual(signature))
            {
                return data;
            }
        }
    }
    catch
    {
        // error
    }
    return "";
}

private static string FixBase64String(string str)
{
    while (str.Length % 4 != 0)
    {
        str = str.PadRight(str.Length + 1, '=');
    }
    return str.Replace("-", "+").Replace("_", "/");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top