I'm able to successfully validate the signed request for a Facebook canvas app using the example here, but I'm unable to decode the payload. The Facebook documentation states that the 2nd parameter in signed_request is a base64url encoded JSON object. In PHP the payload is decoded using json_decode:
$data = json_decode(base64_url_decode($payload), true);
What is the equivalent in C#?
Sorry, bit of a StackOverflow noob, but for anyone trying to use JohnK's method to decode, it works brilliantly, just a couple of implementation tips for anyone like me and the others with the base64 encoding issue....
The Json reference is also available from nuGet
http://developers.facebook.com/docs/guides/canvas/#auth explains the ["signed_request"] element in more detail, but put simply, when Facebook posts back (in my case after a user registration request), you can get the data from the post, but the string is in TWO PARTS, separated by a '.' - As such, trying to decode ["signed_request"] will fail as '.' isn't a Base64 char. The first part is the signature to allow you to validate that the post came from Facebook (only us and them know the sig to decode) and the second is the payload.
So, I got this to work with the following code (in a MVC controller), source is a Facebook registration button....
and then the Controller code responds to the registration request
hope this helps someone, and sorry if this should have been edit/feedback or whatever...
Same code but without Json.NET dependency:
You can use it like this:
i have change the DecodePayload by this and it work fine for me:
Here's how to do it using Facebook SDK
The following should help you out..
(Note: The JObject reference is from JSON.NET available via http://james.newtonking.com/projects/json-net.aspx and http://json.codeplex.com/)
Namespaces used:
Code:
It is what I'm using in FaceSharp.. hope it helps
Check out the Facebook .Net SDK on Codeplex http://facebooksdk.codeplex.com. It will handle all the 'dirty work' for you. For example, I could call the following code either from a controller action or on Page_Load.
Thats it. You don't really need to worry about how facebook is returning the data to you or decoding it. The SDK handles all that for you.