Пример #1
0
        /// <summary>
        /// 客户端转换为字符串
        /// </summary>
        /// <returns></returns>
        public string ToClientString()
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();

            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            this.Key = key;//客户端Key

            var json = JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.None, timeFormat);
            //替换 key ,sign
            string sign1    = ",\"Sign\":null";
            string sign2    = "\"Sign\":null,";
            string sign3    = "\"Sign\":null";
            string tempJson = json.Replace(sign1, "").Replace(sign2, "").Replace(sign3, "");

            tempJson = JsonSort.SortJson(JToken.Parse(tempJson), null);
            string sign = MD5Helper.GetMD5(tempJson + secret);

            this.Sign = sign;

            timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            json = JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.None, timeFormat);
            return(json);
        }
Пример #2
0
        public override string ToString()
        {
            var json = JsonConvert.SerializeObject(this);
            //替换 key ,sign
            string sign1    = ",\"Sign\":null";
            string sign2    = "\"Sign\":null,";
            string sign3    = "\"Sign\":null";
            string tempJson = json.Replace(sign1, "").Replace(sign2, "").Replace(sign3, "");

            tempJson = JsonSort.SortJson(JToken.Parse(tempJson), null);
            string sign = MD5Helper.GetMD5(tempJson + secret);

            this.Sign = sign;
            json      = JsonConvert.SerializeObject(this);
            return(json);
        }
Пример #3
0
        /// <summary>
        /// 取Sign
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected SignParams GetSign(string token, object obj, string action)
        {
            long    timestamp = ToolsCommon.GetTimestamp();
            JObject jObj      = JObject.FromObject(obj);

            jObj.Add("app_key", EleConfig.APP_KEY);
            jObj.Add("timestamp", timestamp);
            string sortJson = JsonSort.SortJson(jObj, null);

            ////替换掉属性排序关键字
            //Regex reg = new Regex("[a-z]__");
            //sortJson = reg.Replace(sortJson, "");
            jObj = JObject.Parse(sortJson);
            List <JProperty> listProperty = jObj.Properties().ToList();
            StringBuilder    buff         = new StringBuilder();

            foreach (JProperty attr in listProperty)
            {
                string pKey   = attr.Name;
                string pValue = attr.Value.ToString(Newtonsoft.Json.Formatting.None);
                if (attr.Value.Type == JTokenType.String)
                {
                    pValue = "\"" + attr.Value.ToString() + "\"";
                }
                else if (attr.Value.Type == JTokenType.Float)
                {
                    pValue = float.Parse(pValue).ToString();
                }
                else if (attr.Value.Type == JTokenType.Object)
                {
                }
                buff.Append(pKey + "=" + pValue);
            }

            //完整拼接字符串
            string     fullParams  = action + token + buff.ToString() + EleConfig.APP_SECRET;
            string     sign        = ToolsCommon.GetMD5(fullParams).ToUpper();
            SignParams pSignParams = new SignParams();

            pSignParams.sign      = sign;
            pSignParams.timestamp = timestamp;
            pSignParams.token     = token;
            pSignParams.action    = action;
            return(pSignParams);
        }