如何解码C#中Canvas signed_request的OAuth 2.0? [英] How to decode OAuth 2.0 for Canvas signed_request in C#?

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

问题描述

我可以使用示例这里,但是我无法解码有效载荷。 Facebook文档指出,signed_request中的第二个参数是一个base64url编码的JSON对象。在PHP中,使用json_decode解码有效载荷:

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

C#中的等价物是什么?

解决方案

以下内容可以帮助您。



注意:JObject引用来自JSON.NET,可通过 http:/ /james.newtonking.com/projects/json-net.aspx http://json.codeplex.com /



使用的命名空间:

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

代码:

  public Dictionary< string,string> DecodePayload(string payload)
{
var encoding = new UTF8Encoding();
var decodedJson = payload.Replace(=,string.Empty).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] ??);

返回参数;
}

这是我在FaceSharp中使用的..希望它有助于/ p>

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);

What is the equivalent in C#?

解决方案

The following should help you out..

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

Namespaces used:

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;
    }

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

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

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