如何去code的OAuth 2.0帆布signed_request在C#中? [英] How to decode OAuth 2.0 for Canvas signed_request in C#?

查看:131
本文介绍了如何去code的OAuth 2.0帆布signed_request在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功验证使用例如<一个一个Facebook应用程序的画布的签名的请求href=\"http://stackoverflow.com/questions/3385593/c-hmacsha256-problem-matching-facebook-signed-request-implementation\">here,但我无法去code中的有效载荷。 Facebook的文件指出,在signed_request第二个参数是一个base64url连接codeD JSON对象。在PHP中的有效载荷使用json_de code德codeD:

I'm able to successfully validate the signed request for a Facebook canvas app using the example here, but I'm unable to decode the payload. The Facebook documentation states that the 2nd parameter in signed_request is a base64url encoded JSON object. In PHP the payload is decoded using json_decode:

$data = json_decode(base64_url_decode($payload), true);

什么是在C#中等价?

What is the equivalent in C#?

推荐答案

下面应该帮助你。

注意:本JObject引用是从JSON.NET通过<一个可用href=\"http://james.newtonking.com/projects/json-net.aspx\">http://james.newtonking.com/projects/json-net.aspx和 HTTP://json.$c$cplex.com/

(Note: The JObject reference is from JSON.NET available via http://james.newtonking.com/projects/json-net.aspx and http://json.codeplex.com/)

命名空间:

using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq; // JSON.NET project 

code:

public Dictionary<string,string> DecodePayload(string payload)
    {
        var encoding = new UTF8Encoding();
        var decodedJson = payload.Replace("=", string.Empty).Replace('-', '+').Replace('_', '/');
        var base64JsonArray = Convert.FromBase64String(decodedJson.PadRight(decodedJson.Length + (4 - decodedJson.Length % 4) % 4, '='));
        var json = encoding.GetString(base64JsonArray);
        var jObject = JObject.Parse(json);

        var parameters = new Dictionary<string, string>();
        parameters.Add("user_id", (string)jObject["user_id"] ?? "");
        parameters.Add("oauth_token", (string)jObject["oauth_token"] ?? "");
        var expires = ((long?) jObject["expires"] ?? 0);
        parameters.Add("expires", expires > 0 ? expires.ToString() : "") ;
        parameters.Add("profile_id", (string)jObject["profile_id"] ?? "");

        return parameters;
    }

这是我使用的FaceSharp ..什么希望它能帮助

It is what I'm using in FaceSharp.. hope it helps

这篇关于如何去code的OAuth 2.0帆布signed_request在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆