2011-05-26 115 views
1

我正在使用c#4.0,我正在整合facebook註冊插件 有人可以告訴我我需要做什麼才能閱讀已簽名的請求。 我已經下載Facebook.dll和Newtonsoft.Json.dll 我也有有效 應用程序ID:* ** API密鑰:* ** * ** * *** 應用揭祕: ** * ** * ** * **** 如果可能的話p租賃給我一個示例代碼,我應該如何通過這些關鍵字,以及如何收集以簽名請求形式發送的解碼數據。如何使用c#4.0讀取簽名的請求

感謝:

+0

可能重複http://stackoverflow.com/questions/4575932/read-oauth2-0-signed-request-facebook-registration-c-mvc) – bkaid 2011-05-26 14:33:55

回答

4

必須有更多的簡便的方法來閱讀以下是我所使用簽名的請求。在c#中閱讀Facebook簽名請求的步驟很少。

  1. Dot net 2010需要遵循以下步驟。建議您在完成後創建一個名爲「fb」的新的基於Web的項目,然後您可以將此代碼導入到您的實際項目中。
  2. 下載從http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs
  3. 源解壓,你會得到「facebooksdk-f8109846cba5」裏面,你會發現一個文件夾facebooksdk-f8109846cba5 \來源\ Facebook在此文件夾中查找「Facebook.csproj」
  4. 打開「臉譜之後.csproj的」在VS 2010中查找文件‘FacebookApp.cs’打開此文件,然後搜索「內部保護FacebookSignedRequest ParseSignedRequest(字符串signedRequestValue)
  5. 改變‘內部保護’到‘大衆’
  6. 然後用鼠標右鍵單擊生成項目在項目上。現在它的編譯文件(「Facebook.dll」)已經可以使用了。將其複製到您的項目bin目錄並添加其引用。
  7. 現在從http://json.codeplex.com/releases/view/50552下載Json.Net 3.5版本8並添加到您的項目bin文件夾中,並添加其參考。
  8. 現在您已準備好閱讀已簽名的請求。現在是編寫代碼來讀取簽名請求的時候了。

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. 這是我使用和它的工作對我來說很好。
+0

Shahid,你很好回答你自己的問題。請注意,你也可以接受它。這樣,每個人都會看到你的問題得到了回答。 – Marijn 2011-06-03 07:21:51

0

這裏使用FB C#SDK v.5.2.1.0

var signed_request = Request.Form["signed_request"]; 

var fap = Facebook.FacebookApplication.Current; 
var signed_request_obj = Facebook.FacebookSignedRequest.Parse(fap, signed_request); 
if (signed_request_obj != null) 
{ 
    JObject o = JObject.Parse(signed_request_obj.Data.ToString()); 
} 
[讀取的OAuth2.0 Signed_Request登記的Facebook C#MVC(的