如何使用c#4.0读取签名请求

本文关键字:请求 读取 何使用 c#4 | 更新日期: 2023-09-27 17:57:38

我使用的是c#4.0,我正在集成facebook注册插件有人能告诉我我需要做什么来阅读签名的请求吗。我已经下载了Facebook.dll和Newtonsoft.Json.dll我也有有效的应用程序ID:***API密钥:********应用程序秘密:************如果可能的话,请给我一个示例代码,我应该如何传递这些密钥,以及如何收集以签名请求形式发送的解码数据。

感谢:

如何使用c#4.0读取签名请求

必须有更简单的方法来读取签名请求。以下是我正在使用的方法。有几个步骤涉及阅读脸书签名请求在c#

  1. Dot-net2010需要遵循这些步骤。建议你在完成时制作一个名为"fb"的新的基于web的项目,然后你可以将此代码导入到你的真实项目中
  2. 从下载源http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs
  3. 解压缩后,你会在里面找到"facebooksdk-f8109846cba5",你会发现一个文件夹facebooksdk-f8109846cba5''Source''Facebook在这个文件夹中查找"Facebook.csproj"
  4. 在vs 2010中打开"Facebook.csproj"查找文件"FacebookApp.cs"打开此文件并搜索"内部保护的FacebookSignedRequest ParseSignedRequest(字符串signedRequestValue)"
  5. 将"内部保护"更改为"公共"
  6. 然后通过右键单击项目来构建项目。现在它的编译文件("Facebook.dll")可以使用了。将其复制到您的项目bin目录并添加其引用
  7. 现在下载Json.Net 3.5版本8http://json.codeplex.com/releases/view/50552并添加到您的项目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. 这就是我正在使用的,它对我来说很好

这里使用的是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());
}