php hash_hmac到c# - webresponse“NONCE_NOT_PROVIDED”

I try to convert a php-code to c#

php

$apikey='xxx';
$apisecretkey='yyy';
$nonce=time();
$uri='https://website.com/api/getsomething?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecretkey);
$resource = curl_init($uri);
curl_setopt($resource, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($resource);
$obj = ($execResult);

and here my C#

WebRequest _WebRequest;
WebResponse _WebResponse;
string apikey = "xxx";
string apisecret = "yyy";
string nonce = ((int)(DateTime.UtcNow - new DateTime(1970,1,1)).TotalSeconds).ToString();
string uri = $"https://website.com/api/getsomething?apikey={apikey}nonce={nonce}";
string _Hash_hmac = Hash_hmac(uri, apisecret);

_WebRequest = WebRequest.Create(uri);
_WebRequest.Headers["apisign"] = _Hash_hmac;

_WebResponse = _WebRequest.GetResponse();

The Code for Hash-hmac I use this C# equivalent to hash_hmac in PHP

The Webresponse is always "NONCE_NOT_PROVIDED"

I checked the outcome from the hash_hmac with the one in php - they are the same. And also try string ToUpper and ToLower.

What I am doing wrong?

dqwnxdhb88531
dqwnxdhb88531 祝你好运@GustavoBesade
2 年多之前 回复
dotibrb048760
dotibrb048760 问题解决了。不得不将HMACSHA256更改为HMACSHA512来计算哈希值。它没有写在API的文档中感谢您的帮助
2 年多之前 回复
dp20011
dp20011 这可能是由于您传递的参数。您应该调试代码(php和c#),因为您在c#中传递的日期值采用不同的格式。
2 年多之前 回复
dpw70180
dpw70180 我认为'&'它是PHP语法的一部分。现在它看起来更好。得到“INVALID_SIGNATURE”
2 年多之前 回复
dongsu2807
dongsu2807 替换线字符串uri=$“website.com/api/getsomething?apikey={apikey}nonce={nonce}”;用这个字符串uri=$“website.com/api/getsomething?apikey={apikey}&nonce={nonce}”;
2 年多之前 回复

1个回答

Aren't you missing an ampersand (&)?

apikey={apikey}nonce={nonce}

becomes

apikey={apikey}&nonce={nonce}

I would advise reading your code more closely with these kinds of errors.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐