请问c# OpenAuth2提示“签名错误!验证失败”是什么原因?多谢
金蝶云社区-云社区用户22A77023
云社区用户22A77023
0人赞赏了该文章 1,340次浏览 未经作者许可,禁止转载编辑于2017年02月22日 23:50:13

Random RAND = new Random();
long timestamp1 = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
long nonce1 = timestamp1 + RAND.Next();
string secretkey = "18611177023";
string appid = "500002646";

string[] arr = new string[] { appid, nonce1.ToString(), timestamp1.ToString(), "1.1", secretkey };
Array.Sort(arr);
string data = string.Empty;
foreach (var key in arr)
{
data += key.ToString();
}

data = SHA1_To(data);

string authorization = "OpenAuth2 version=\"1.1\", appid=\"500002646\", timestamp=" + timestamp1.ToString() + ", nonce=\"" + nonce1.ToString() + "\", sign=\"" + data +"\"";

string aa = HttpPost("http://do.yunzhijia.com/openauth2/api/appAuth2", "", authorization);

public static string SHA1_To(string str)
{
byte[] StrRes = Encoding.Default.GetBytes(str);
HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
StrRes = iSHA.ComputeHash(StrRes);
StringBuilder EnText = new StringBuilder();
foreach (byte iByte in StrRes)
{
EnText.AppendFormat("{0:x2}", iByte);
}
return EnText.ToString();
}

public static string HttpPost(string url, string dataStr, string data)
{
byte[] bytes = Encoding.UTF8.GetBytes(dataStr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Headers.Add("authorization", data);
request.ContentLength = bytes.Length;

using (Stream stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}