private int delayMinits = 10;//请求延迟时间10分钟 public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext == null) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden); return; } SignParse parse = new SignParse(); // RequestData reqData = parse.GetRequestParams((Microsoft.AspNetCore.Http.HttpContext)actionContext); base.OnActionExecuting(actionContext); }
public IRestResponse HttpGet(string resource, Dictionary <string, string> paramData) { var client = new RestClient(_baseUri); var request = new RestRequest(resource, Method.GET); request.AddHeader("Content-Type", "application/json; charset=utf-8"); //获取所有请求参数 SortedDictionary <string, string> sortParam = new SortedDictionary <string, string>(); foreach (var pair in paramData) { request.AddParameter(pair.Key, pair.Value); sortParam.Add(pair.Key, pair.Value); } //获取请求方式相关参数 long unixTime = TimeHelper.dtDateTime2Unix(); sortParam.Add("timestamp", unixTime.ToString()); sortParam.Add("key", App_Key); sortParam.Add("method", request.Method.ToString()); Uri uri = client.BuildUri(request); sortParam.Add("uri", uri.PathAndQuery); sortParam.Add("contentlength", "0"); //计算sign值 string mySign = SignParse.CreateRequestMySign(sortParam, App_Secret); //请求头中添加三个必要参数 key sign timestamp request.AddHeader("X-Auth-Sign", mySign); request.AddHeader("X-Auth-Key", App_Key); request.AddHeader("X-Auth-TimeStamp", unixTime.ToString()); //发起请求 IRestResponse queryResult = client.Execute(request); return(queryResult); }
public IRestResponse HttpPost(string resource, object postData) { var client = new RestClient(_baseUri); var request = new RestRequest(resource, Method.POST); request.AddHeader("Content-Type", "application/json; charset=utf-8"); //将请求内容作为json数据存入Body中 request.AddJsonBody(postData); //获取请求数据的字节长度 string json_Data = JsonConvert.SerializeObject(postData); var bytes = Encoding.UTF8.GetBytes(json_Data); //获取所有请求参数 SortedDictionary <string, string> sortParam = new SortedDictionary <string, string>(); //获取请求方式相关参数 long unixTime = TimeHelper.dtDateTime2Unix(); sortParam.Add("timestamp", unixTime.ToString()); sortParam.Add("key", App_Key); sortParam.Add("method", request.Method.ToString()); Uri uri = client.BuildUri(request); sortParam.Add("uri", uri.PathAndQuery); sortParam.Add("contentlength", bytes.Length.ToString()); //计算sign值 string mySign = SignParse.CreateRequestMySign(sortParam, App_Secret); //请求头中添加三个必要参数 key sign timestamp request.AddHeader("X-Auth-Sign", mySign); request.AddHeader("X-Auth-Key", App_Key); request.AddHeader("X-Auth-TimeStamp", unixTime.ToString()); IRestResponse queryResult = client.Execute(request); return(queryResult); }
/// <summary> /// 基于RestSharp封装 /// </summary> /// <param name="client"></param> /// <param name="request"></param> /// <param name="method"></param> /// <param name="paramData"></param> /// <param name="bodyData"></param> /// <returns></returns> private RestRequest BaseApiRequest(RestClient client, RestRequest request, Method method, Dictionary <string, string> paramData = null, object bodyData = null) { int contentLength = 0; //获取所有请求参数 SortedDictionary <string, string> sortParam = new SortedDictionary <string, string>(); switch (method) { case Method.GET: case Method.DELETE: if (paramData != null) { foreach (var pair in paramData) { request.AddParameter(pair.Key, pair.Value); sortParam.Add(pair.Key, pair.Value); } } contentLength = 0; break; case Method.POST: case Method.PUT: if (bodyData != null) { //将请求内容作为json数据存入Body中 request.AddJsonBody(bodyData); //获取请求数据的字节长度 //string json_Data = JsonConvert.SerializeObject(bodyData); string json_Data = SimpleJson.SerializeObject(bodyData); var bytes = Encoding.UTF8.GetBytes(json_Data); contentLength = bytes.Length; } break; case Method.HEAD: break; case Method.OPTIONS: break; case Method.PATCH: break; case Method.MERGE: break; default: break; } request.AddHeader("Content-Type", "application/json; charset=utf-8"); //获取请求方式相关参数 long unixTime = TimeHelper.dtDateTime2Unix(); sortParam.Add("timestamp", unixTime.ToString()); sortParam.Add("key", _appKey); sortParam.Add("method", request.Method.ToString()); Uri uri = client.BuildUri(request); sortParam.Add("uri", uri.PathAndQuery); sortParam.Add("contentlength", contentLength.ToString()); //计算sign值 string mySign = SignParse.CreateRequestMySign(sortParam, _appSecret); //请求头中添加三个必要参数 key sign timestamp request.AddHeader("X-Auth-Sign", mySign); request.AddHeader("X-Auth-Key", _appKey); request.AddHeader("X-Auth-TimeStamp", unixTime.ToString()); return(request); }
private int delayMinits = 10;//请求延迟时间2分钟 public override void OnActionExecuting(HttpActionContext actionContext) { if (actionContext == null) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden); return; } SignParse parse = new SignParse(); RequestData reqData = parse.GetRequestParams(); string Ckey = reqData.ReqKey; string Csign = reqData.ReqSign; string Ctime = reqData.ReqTimeStamp; var Cdis = reqData.ReqDics; #region 验证是否超时 long deM = TimeHelper.dtTimeSpan(Convert.ToInt64(Ctime), DateTime.Now, "M"); if (deM > delayMinits) { //请求超时,无效 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, new { code = 4002, msg = "Request Timeout" }); return; } #endregion #region 验证appKey if (string.IsNullOrEmpty(Ckey)) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, new { code = 4003, msg = "key is Empty" }); return; } //从数据库中查询AppKey和AppSecret Showkey ishavekey = showKeys.Where(p => p.key == Ckey).FirstOrDefault(); if (ishavekey == null) { //key不存在 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, new { code = 4004, msg = "key is Error" }); return; } #endregion #region 验证Sign string thisSecrret = ishavekey.secret; bool isSign = parse.VerifySign(Cdis, Csign, thisSecrret); if (!isSign) { //sign验证失败 actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Forbidden, new { code = 4005, msg = "Sign is Error" }); return; } #endregion base.OnActionExecuting(actionContext); }