开发者社区> 问答> 正文

调用云解析API,总是报SignatureDoesNotMatch错误,求解决

我调用云解析老是报 SignatureDoesNotMatch错误,求解决,程序语言是C#,代码如下:



public class AliyunDNSUtil
    {
        public static void UpdateDNS(string ip)
        {
            var x = GetDomainList();
        }


        private static string UrlEncoder(string value)
        {
            return HttpUtility.UrlEncode(value).Replace("+", "%20").Replace("*", "%2A").Replace("%7E", "~");
        }


        private static SortedDictionary<string, string> _PublicArguments;
        private static SortedDictionary<string, string> PublicArguments
        {
            get
            {
                if (_PublicArguments == null)
                {
                    var result = new SortedDictionary<string, string>();


                    result.Add("Format", UrlEncoder("XML"));
                    result.Add("Version", UrlEncoder("2015-01-09"));
                    result.Add("AccessKeyId", UrlEncoder(Settings.AliyunAccessKeyID));
                    result.Add("SignatureMethod", UrlEncoder("HMAC-SHA1"));
                    result.Add("Timestamp", UrlEncoder(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")));
                    result.Add("SignatureVersion", UrlEncoder("1.0"));
                    result.Add("SignatureNonce", UrlEncoder(Guid.NewGuid().ToString("D")));


                    _PublicArguments = result;
                }
                return _PublicArguments;
            }
        }


        private static string StringToSign(string value)
        {
            Encoding encode = Encoding.UTF8;
            byte[] byteData = encode.GetBytes(value);
            byte[] byteKey = encode.GetBytes(Settings.AliyunAccessKeySecret + "&");
            HMACSHA1 hmac = new HMACSHA1(byteKey);
            CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
            cs.Write(byteData, 0, byteData.Length);
            cs.Close();
            return Convert.ToBase64String(hmac.Hash);
        }


        private static XDocument GetDomainList()
        {
            var arguments = new SortedDictionary<string, string>();
            foreach (var item in PublicArguments.Keys)
            {
                arguments.Add(item, PublicArguments[item]);
            }
            arguments.Add("Action", UrlEncoder("DescribeDomains"));
            arguments.Add("KeyWord", UrlEncoder(Settings.DomainName));


            var argumentssb = new StringBuilder();
            foreach (var item in arguments.Keys)
            {
                argumentssb.Append("&" + item + "=" + arguments[item]);
            }


            var argumentsstr = argumentssb.ToString().Substring(1);
            var signstring = "GET&%2F&" + UrlEncoder(argumentsstr);
            var encodedargs = argumentsstr + "&Signature=" + UrlEncoder(StringToSign(signstring));
            var url = "https://dns.aliyuncs.com/?" + encodedargs;
            var xmlstream = HttpRequest(url);
            if (xmlstream == null)
            {
                return null;
            }
            var reader = new StreamReader(xmlstream, Encoding.UTF8);
            var str = reader.ReadToEnd();


            return XDocument.Load(xmlstream);
        }


        private static Stream HttpRequest(string url)
        {
            try
            {
                var httpReq = (HttpWebRequest)WebRequest.Create(url);
                var httpResp = (HttpWebResponse)httpReq.GetResponse();
                return httpResp.GetResponseStream();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

展开
收起
alfadb 2015-03-31 16:51:39 11268 0
3 条回答
写回答
取消 提交回答
  • 回 1楼rfan的帖子
    2015-11-08 10:47:06
    赞同 展开评论 打赏
  • 回 楼主(alfadb) 的帖子
    造成此问题的原因是参数没有严格按照大小写排序造成的。
    SortedDictionary<String, String> parameters = new SortedDictionary<string, string>( StringComparer.Ordinal);

    可参考: http://bbs.aliyun.com/read/198526.html?page=e
    2015-04-01 13:56:15
    赞同 展开评论 打赏
  • Re调用云解析API,总是报SignatureDoesNotMatch错误,求解决
    参考我的python实现:关系Go!中一文的万网(阿里云)DDNS客户端
    2015-04-01 10:41:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像