Search code examples
c#facebooksdkrequestsigned

How to read signed request using c# 4.0


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:


Solution

  • 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#

    1. Dot net 2010 is required to follow these steps. It is suggested if you make a new web based project named “fb” when you done then you may import this code to your real project.
    2. Download source from http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs
    3. After unzip you will get “facebooksdk-f8109846cba5” inside it you will find a folder facebooksdk-f8109846cba5\Source\Facebook in this folder look for “Facebook.csproj”
    4. Open “Facebook.csproj” in vs 2010 Look for file “FacebookApp.cs” open this file and search “internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)
    5. Change “internal protected” to “public”
    6. Then build the project by right click on project. Now it’s complied file (“Facebook.dll”) is ready to use. Copy it to your project bin directory and add its reference.
    7. Now download Json.Net 3.5 release 8 from http://json.codeplex.com/releases/view/50552 and add to your project bin folder and also add its reference.
    8. Now you are ready to read signed request. It is time to write code to read signed request.
    
    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; } } }
    1. This is what I am using and its working fine for me.