Simple Way To Check If User Has Liked Your Facebook Page with ASP.NET

Facebook provides a signed request with every postback to your page.  Inside that request is some 
basic info, including whether
or not the user has liked the profile page that is hosting the canvas
application. Here’s a quick way to retrieve it:
bool liked = false;
string signed_request = Request["signed_request"];
 
if (!String.IsNullOrEmpty(signed_request))
{
    string payload = signed_request.Split('.')[1];
    var encoding = new UTF8Encoding();
    var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
    var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
    var json = encoding.GetString(base64JsonArray);
    JObject obj = JObject.Parse(json);
    liked = (bool)obj.SelectToken("page.liked");
}

 

JObject courtesy of Newtonsoft.Json.

2 Comments

Comments have been disabled for this content.