我正在使用 C# 4.0 并且正在集成 Facebook 注册插件 有人可以告诉我我需要做什么才能阅读签名的请求。 我已经下载了 Facebook.dll 和 Newtonsoft.Json.dll 我也有有效 应用 ID:*** API key :********** 应用密码:************ * 如果可能的话,请给我一个示例代码,我应该如何传递这些 key 以及如何收集以签名请求的形式发送的解码数据。
感谢:
最佳答案
必须有更简单的方法来阅读签名的请求,下面是我正在使用的方法。在 c# 中读取 facebook 签名请求涉及的步骤很少
- 需要 Dot net 2010 才能执行这些步骤。建议您在完成后创建一个名为“fb”的基于 Web 的新项目,然后您可以将此代码导入到您的实际项目中。
- 从http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs下载源代码
- 解压后你会在里面得到“facebooksdk-f8109846cba5”你会发现一个文件夹facebooksdk-f8109846cba5\Source\Facebook 在这个文件夹中寻找“Facebook.csproj”
- 在 vs 2010 中打开“Facebook.csproj”查找文件“FacebookApp.cs”打开此文件并搜索“internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)”
- 将“internal protected”改为“public”
- 然后通过右键单击项目来构建项目。现在它的编译文件(“Facebook.dll”)可以使用了。将其复制到您的项目 bin 目录并添加其引用。
- 现在从 http://json.codeplex.com/releases/view/50552 下载 Json.Net 3.5 第 8 版并添加到您的项目 bin 文件夹并添加其引用。
- 现在您可以阅读签名的请求了。是时候编写代码来读取已签名的请求了。
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; }
}
}
- 这就是我正在使用的,对我来说效果很好。
关于c# - 如何使用 C# 4.0 读取签名请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6135707/