Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法执行C#Restful身份验证(API密钥/机密正确)_C#_Rest_Web_Http Post_Restful Authentication - Fatal编程技术网

无法执行C#Restful身份验证(API密钥/机密正确)

无法执行C#Restful身份验证(API密钥/机密正确),c#,rest,web,http-post,restful-authentication,C#,Rest,Web,Http Post,Restful Authentication,我想将JS的经过身份验证的REST请求(如下所示)转换为C代码——使用RestSharp库(如图所示),我似乎没有正确构建签名。请帮忙 Rest响应使用“无效API密钥”保留响应(确认密钥/密码正确) 我已经写了下面的C代码,但不起作用,请指出,我猜签名楼是不正确的 希望这足够清楚。提前谢谢。您知道这两个http调用之间有什么不同吗?它肯定使用https:例如。我不知道,我试着把它转换成C#并做一些类似的事情,但反应出乎意料。 // sample code using the crypt

我想将JS的经过身份验证的REST请求(如下所示)转换为C代码——使用RestSharp库(如图所示),我似乎没有正确构建签名。请帮忙

Rest响应使用“无效API密钥”保留响应(确认密钥/密码正确)

我已经写了下面的C代码,但不起作用,请指出,我猜签名楼是不正确的


希望这足够清楚。提前谢谢。

您知道这两个http调用之间有什么不同吗?它肯定使用https:例如。我不知道,我试着把它转换成C#并做一些类似的事情,但反应出乎意料。

// sample code using the crypto lib to build the RESTFUL request
const crypto = require('crypto')
const request = require('request')

const apiKey = 'test'
const apiSecret = 'test'
const url = 'version/auth/abc'
const nonce = Date.now().toString()
const body = {}
const rBody = JSON.stringify(body)

// try build the RESTFUL signature here using crypto lib and use sha384

// Not quite sure what's the update does here though.
let signature = `/api/${url}${nonce}${rBody}`
signature = crypto
  .createHmac('sha384', apiSecret)
  **.update(signature)**
  .digest('hex')

// all code above is fine. And this is the sample code only, and trying to do something same in C# and have tried the following. I believe is the way create the "signature" issue.


    private uint64 _nonce = UnixTimestamp.CurrentTimeInMillis;
    public uint64 MyNonce()
    {
       return ++_nonce;
    }

    private string MySignature(string jsonData)
    {
        byte[] data = Encoding.UTF8.GetBytes(jsonData);
        _hasher = _hasher ?? new HMACSHA384(Encoding.UTF8.GetBytes(PrivateApiSecret));
        byte[] hash = _hasher.ComputeHash(data);            
                return GetHexString(hash);
    }


    // try to build the request below in a method.
    // only show method body below:
    // using the RestSharp client to build Restful req.
    var client = new RestClient(new Uri("the API address goes here"));
    var request = new RestRequest(string.Format("auth/abc"), Method.POST);

    // try do something similar and serialize to json in order to build the signature.
    Dictionary emptyBody= new Dictionary();
    string url = "version/auth/abc";
    string rawBody = JsonConvert.SerializeObject(emptyBody);
    string sign = $"/api/{url}{MyNonce()}{rawBody}";

    string sign64= Convert.ToBase64String(Encoding.UTF8.GetBytes(sign));                
    string signature = MySignature(sign64);

    // add the key and signature above to the header of reqest.
    request.AddHeader("nonce", GetNonce());
    request.AddHeader("apikey", PrivateApiKey);
    request.AddHeader("signature", signature);

    // well, you don't need me to explain this line don't you?
    var response = request.Execute(....);