예제 #1
0
        //签名验证
        public bool CheckSign(string jsonStr)
        {
            if (null == jsonStr && jsonStr.Length <= 0)
            {
                return(false);
            }
            JObject jo     = JObject.Parse(jsonStr);
            var     result = "";

            if (jo.Property("result") != null)
            {
                object resultTemp = jo["result"];
                result = JsonConvert.SerializeObject(resultTemp).Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\\", "");
            }
            string status = jo["state"].ToString();
            string ts     = jo["ts"].ToString();
            string ext    = "";

            if (null != jo["ext"])
            {
                ext = JsonConvert.SerializeObject(jo["ext"]).Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\t", "");
            }
            string resultStr  = secretKey + status + result + ts + ext + secretKey;
            string signString = new MD5Hashing().HashString(resultStr);
            string sign       = jo["sign"].ToString();

            if (signString.Equals(sign))
            {
                return(true);
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// 给请求签名。
        /// </summary>
        /// <param name="parameters">所有字符型的请求参数</param>
        /// <param name="body">请求主体内容</param>
        /// <param name="secret">签名密钥</param>
        /// <param name="signMethod">签名方法md5</param>
        /// <returns>签名</returns>
        public static string SignTopRequest(IDictionary <string, string> parameters, string body, string secret, string signMethod)
        {
            // 第一步:把字典按Key的字母顺序排序
            IDictionary <string, string> sortedParams = new SortedDictionary <string, string>(parameters, StringComparer.Ordinal);

            // 第二步:把所有参数名和参数值串在一起
            StringBuilder query = new StringBuilder();

            query.Append(secret);
            foreach (KeyValuePair <string, string> kv in sortedParams)
            {
                if (!string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value))
                {
                    query.Append(kv.Key).Append(kv.Value);
                }
            }
            // 第三步:把请求主体拼接在参数后面
            if (!string.IsNullOrEmpty(body))
            {
                query.Append(body);
            }
            // 第四步:使用MD5算法加密,并返回加密结果
            query.Append(secret);
            MD5Hashing md5 = new MD5Hashing();

            return(md5.HashString(query.ToString()));
        }