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

标签 c# facebook sdk request signed

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

感谢:

最佳答案

必须有更简单的方法来阅读签名的请求,下面是我正在使用的方法。在 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\Source\Facebook 在这个文件夹中寻找“Facebook.csproj”
  4. 在 vs 2010 中打开“Facebook.csproj”查找文件“FacebookApp.cs”打开此文件并搜索“internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)”
  5. 将“internal protected”改为“public”
  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 + "<br/>");
            Response.Write(ud.birthday + "<br/>");
            Response.Write(ud.country + "<br/>");
            Response.Write(ud.email + "<br/>");
            Response.Write(ud.gender + "<br/>");

            Response.Write(ud.location + "<br/>");
            Response.Write(ud.userId + "<br/>");




        }



    }



    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. 这就是我正在使用的,对我来说效果很好。

关于c# - 如何使用 C# 4.0 读取签名请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135707/

相关文章:

java - 无法定位 Javac 编译器 maven sonar :sonar

java - Java Card 是否支持生物识别(特别是指纹认证)?

java - 在没有第 3 方库的情况下在 Java 中使用 C# DLL

c# - 从 USB-GPS-Receiver 读取数据

Facebook 用于图形 API 的新 PHP SDK - 多查询

Facebook OAuthException 代码 190 子代码 490 - 用户注册到阻塞的登录检查点

android - 如何使用Android库从应用程序获取资源路径?

c# - 按 Contains(variable) 过滤时 LINQ Where 的意外输出

c# - Fluent NHibernate,联合子类映射

ruby-on-rails - Ruby on Rails Youtube图像嵌入