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.

Published Wednesday, October 26, 2011 2:43 PM by fallen888

Comments

# Simple Way To Check If User Has Liked Your... | ASP.NET, C#, Web | Syngu

Pingback from  Simple Way To Check If User Has Liked Your... | ASP.NET, C#, Web | Syngu

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

Sunday, October 30, 2011 12:13 PM by prezzy

thank you. this will be very helpful for me

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

Monday, November 28, 2011 2:55 AM by valse

Thanks!

Nice clean code ;-)

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

Wednesday, January 25, 2012 10:54 PM by Joby Joseph

If you are dealing with facebook app tab page, following code help you to find isfan status

$signed = parse_signed_request($_REQUEST['signed_request'], 'YOUR-APP-SECRET');

if ($signed['page']['liked'] == 1) {

   $fan = true;

} else {

   $fan = false;

}

Visit this link for demo and source code: http://bit.ly/zmaGgv

Leave a Comment

(required) 
(required) 
(optional)
(required)