C# C语言中的NodeJs认证#
我现在花了相当多的时间尝试从C#访问一个api,我的参数似乎通过了,但api说签名不匹配。下面给出了NodeJs中的示例代码,我应该如何在C#中创建签名 这是我用来在C#中创建签名的代码:C# C语言中的NodeJs认证#,c#,node.js,authentication,C#,Node.js,Authentication,我现在花了相当多的时间尝试从C#访问一个api,我的参数似乎通过了,但api说签名不匹配。下面给出了NodeJs中的示例代码,我应该如何在C#中创建签名 这是我用来在C#中创建签名的代码: 你能给我们看看你现在的C代码吗?javascript/node代码有效吗?我更新了一些代码。我听说这个节点代码是由服务提供商编写的。注意,我尝试了ascii和utf8编码。我觉得这与发送参数的“表单:”方式有关,但可能不是因为跳过键api抱怨没有键。。 var nonce=Math.round(new Dat
你能给我们看看你现在的C代码吗?javascript/node代码有效吗?我更新了一些代码。我听说这个节点代码是由服务提供商编写的。注意,我尝试了ascii和utf8编码。我觉得这与发送参数的“表单:”方式有关,但可能不是因为跳过键api抱怨没有键。。
var nonce=Math.round(new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()).getTime()/1000);
var key='Your api key';
var secret='Your api secret';
var client_id='Your client id';
var path='/ac_balance'; // /ac_balance /my_pending_orders /cancel_order
var signature_data=nonce+key+client_id;
var hmac = crypto.createHmac('sha256', secret); // crypto is a module to encrypt data
hmac.write(signature_data);
hmac.end();
var signature = new Buffer(hmac.read()).toString('base64');
// if path='/ac_balance'
request.post({url:'https://api.somewebsite.com'+path, form: {key:key,nonce:nonce,signature:signature}}, function(err,httpResponse,body){ });
var data = nounce + key + clientId;
var signature = CreateToken(data, secret);
...
private string CreateToken(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}