I am using c# 4.0 and I am integrating facebook registration plugin Can somebody please tell me what I need to do to read signed request. I have downloaded Facebook.dll and Newtonsoft.Json.dll I also have valid App ID: *** API Key: ********** App Secret: ************* If possible please give me a sample code how should I pass these keyes and how I can collect the decoded data sent in form of signed request.
Thanks:
There must be more easy ways to read signed request following is what I am using. There are few steps involved to read facebook signed request in c#
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Facebook;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
namespace fb
{
public partial class test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FacebookApp fap = new FacebookApp();
fap.AppId = "************";
fap.AppSecret = "********************";
string requested_Data = Request.Form["signed_request"];
FacebookSignedRequest fsr = fap.ParseSignedRequest(requested_Data);
// string json = JsonConvert.SerializeObject(fsr.Dictionary, Formatting.Indented);
UserData ud = new UserData(fsr);
Response.Write(ud.name + "
");
Response.Write(ud.birthday + "
");
Response.Write(ud.country + "
");
Response.Write(ud.email + "
");
Response.Write(ud.gender + "
");
Response.Write(ud.location + "
");
Response.Write(ud.userId + "
");
}
}
public class UserData
{
public UserData(FacebookSignedRequest fsr)
{
string value = string.Empty;
JObject o;
foreach (string key in fsr.Dictionary.Keys)
{
value = fsr.Dictionary[key];
switch (key)
{
case "user_id":
userId = value;
break;
case "registration":
o = JObject.Parse(value);
name = GetValue(o, "name");
birthday = GetValue(o, "birthday");
email = GetValue(o, "email");
gender = GetValue(o, "gender");
location = GetValue(o, "location.name");
break;
case "user":
o = JObject.Parse(value);
country = GetValue(o, "country");
break;
}
}
}
private string GetValue(JObject o, string token)
{
string ret = string.Empty;
try
{
ret = (string)o.SelectToken(token);
}
catch (Exception ex)
{
throw ex;
}
return ret;
}
public string name { get; set; }
public string birthday { get; set; }
public string gender { get; set; }
public string location { get; set; }
public string country { get; set; }
public string email { get; set; }
public string userId { get; set; }
}
}