/// <summary> /// 检查应用接入的数据完整性 /// 1)检查timestamp 与系统时间是否相差在合理时间内,如10分钟。 /// 2)将appSecret、timestamp、nonce三个参数进行字典序排序 /// 3)将三个参数字符串拼接成一个字符串进行SHA1加密 /// 4)加密后的字符串可与signature对比,若匹配则标识该次请求来源于某应用端,请求是合法的。 /// </summary> /// <param name="signature">加密签名内容</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机字符串</param> /// <param name="appid">应用接入Id</param> /// <returns></returns> public CheckResult ValidateSignature(string signature, string timestamp, string nonce, string appid) { CheckResult result = new CheckResult { Errmsg = "数据完整性检查不通过" }; #region 校验签名参数的来源是否正确 #region 加密后的字符串可与signature对比 string[] arrTmp = { appid, timestamp, nonce }; Array.Sort(arrTmp); string tmpStr = string.Join("", arrTmp); // tmpStr = EncryptHelper.HashString(tmpStr + ConstHelper.UnlockingKey, "MD5"); tmpStr = CommonContext.MD5Encrupt(tmpStr + ConstHelper.UnlockingKey).ToLower(); #endregion if (tmpStr == signature && ValidateUtil.IsNumber(timestamp)) { DateTime dtTime = DateTimeHelper.StampToDateTime(timestamp); double minutes = DateTime.Now.Subtract(dtTime).TotalMinutes; if (minutes > _timspanExpiredMinutes) { result.Errmsg = "签名时间戳失效"; result.Success = false; } else { result.Errmsg = "检验成功"; result.Success = true; } #endregion } return(result); }
// Upload file with device_tokens to Umeng public string UploadContents(string contents) { if (RootJson.Properties().All(p => p.Name != "appkey") || RootJson.Properties().All(p => p.Name != "timestamp")) { throw new Exception("appkey, timestamp needs to be set."); } // Construct the json string JObject uploadJson = new JObject { { "appkey", RootJson.GetValue("appkey") }, { "timestamp", RootJson.GetValue("timestamp") }, { "content", contents } }; // Construct the request string url = Host + UploadPath; string postBody = uploadJson.ToString(); string sign = CommonContext.MD5Encrupt("POST" + url + postBody + AppMasterSecret).ToLower(); url = url + "?sign=" + sign; var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.UserAgent = UserAgent; request.Timeout = 2 * 60 * 1000; //超时时间设置为两分钟 //request.ContentType = "application/json"; //request.Headers.Set("Pragma", "no-cache"); byte[] postData = Encoding.UTF8.GetBytes(postBody); string retString; using (var requestStream = request.GetRequestStream()) { requestStream.Write(postData, 0, postData.Length); using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"))) { retString = myStreamReader.ReadToEnd(); //ret = responseStream.ReadBytes(); } } } } try { JObject jObject = JObject.Parse(retString); string result = jObject.Property("ret").Value.ToString(); if (result.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase)) { string fileId = jObject.GetValue("data").ToObject <JObject>().GetValue("file_id").ToString(); SetPredefinedKeyValue("file_id", fileId); return(fileId); } else { LogHelper.WriteLog("调用友盟发送失败"); LogHelper.WriteLog(retString); throw new Exception("Failed to upload file"); } } catch (Exception ex) { LogHelper.WriteLog(ex.ToString()); throw ex; } }