C#中的NodeJs身份验证

时间:2016-01-14 11:28:49

标签: c# node.js authentication

我花了不少时间尝试从C#访问api并且已经达到了我的参数似乎通过但是api说签名不匹配的程度。因此,鉴于NodeJs中的示例代码,我应该如何在C#中创建签名?

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

这是我用来在C#中创建签名的代码:

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

0 个答案:

没有答案