简体   繁体   English

在C#中使用Sinch REST Api

[英]Using Sinch REST Api in C#

I am trying to use Sinch REST api in c# for sms verification and I am having trouble encoding the strings required for Authorization. 我正在尝试在C#中使用Sinch REST api进行短信验证,并且在编码授权所需的字符串时遇到了麻烦。 I am using APPLICATION SIGNED REQUEST for Auth and I would like to know what c# functions to use to created the content-Md5 and stringtoSign. 我使用的是Auth的APPLICATION SIGNED REQUEST,我想知道要使用什么c#函数来创建content-Md5和stringtoSign。 For content MD-5 here is the code snippet I have 对于内容MD-5,这是我拥有的代码段

var body = JsonConvert.SerializeObject(smsRequest);

var hs = System.Security.Cryptography.MD5.Create();

var db = hs.ComputeHash(Encoding.UTF8.GetBytes(body));

var stringToSign = Convert.ToBase64String(db);

 public class SmsRequest
{
    [JsonProperty("identity")]
    public Identity Identity;

    [JsonProperty("method")]
    public string Method;

    [JsonProperty("metadata")]
    public Metadata Metadata;
}

public class Metadata
{
    [JsonProperty("os")]
    public string Os;

    [JsonProperty("platform")]
    public string Platform;

}

public class Identity
{
    [JsonProperty("type")]
    public string Type;

    [JsonProperty("endpoint")]
    public string Endpoint;
}

Now for creating the stringtoSign is something I use the following code 现在,我使用以下代码来创建stringtoSign

var stringToSign = "POST" + Environment.NewLine + "jANzQ+rgAHyf1MWQFSwvYw==" + Environment.NewLine +
                  "application/json" + Environment.NewLine + "x-timestamp:2014-06-04T13:41:58Z" +
                  Environment.NewLine + "/v1/sms/+46700000000";//used for signing

var secret = "JViE5vDor0Sw3WllZka15Q=="; // this needs to be signed
var encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(stringToSign);

var hmacsha256 = new HMACSHA256(keyByte);

byte[] messageBytes = Encoding.UTF8.GetBytes(secret);
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);

var res = Convert.ToBase64String(hashmessage);

I am using the values in the example here https://www.sinch.com/using-rest/ and I expect the result to be "qDXMwzfaxCRS849c/2R0hg0nphgdHciTo7OdM6MsdnM=" while using my method I am getting "2/8KqdiC2708EC84vTinPchiATRJiZU4Mgfpykb4F40=". 我在这里的示例中使用的值https://www.sinch.com/using-rest/ ,并且使用我的方法时,我希望结果为“ qDXMwzfaxCRS849c / 2R0hg0nphgdHciTo7OdM6MsdnM =“

What is it that I am doing wrong here? 我在这里做错了什么?

I would use our awesome nuget https://github.com/sinch/nuget-serversdk if you dont want to install the nuget you can check the source and take the pieces you want. 如果您不想安装nuget,可以使用我们的真棒nuget https://github.com/sinch/nuget-serversdk ,您可以检查源代码并获取所需的组件。 its opensource and you 它的开源和你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM