Esempio n. 1
0
 public void Insert(TEntity item)
 {
     if (item != null)
     {
         _db.HashSet(_tableName, item.Id, SerializeMemoryHelper.SerializeToJson(item));
     }
 }
Esempio n. 2
0
 public void Update(TEntity item)
 {
     if (item != null)
     {
         var old = Find(item.Id);
         if (old != null)
         {
             _db.HashDelete(_tableName, item.Id);
             _db.HashSet(_tableName, item.Id, SerializeMemoryHelper.SerializeToJson(item));
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// 直接为APP返回的支付字符串
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="subject"></param>
        /// <param name="total_fee"></param>
        /// <returns></returns>
        public string AppPayFormString(string orderNo, decimal totalFee, string subject = null, string body = "Lind")
        {
            //必填,不需要修改
            Random random = new Random(System.Environment.TickCount);
            //请求号
            string req_id = string.IsNullOrWhiteSpace(orderNo) ? DateTime.Now.ToString("yyyyMMddHHmmss") + random.Next(10000, 99999) : orderNo;

            SortedDictionary <string, object> sParaTemp = new SortedDictionary <string, object>();

            AliPayBussiness bussin = new AliPayBussiness()
            {
                timeout_express = "10m",
                seller_id       = Config.AlipayConfig.Partner,
                product_code    = "QUICK_MSECURITY_PAY",
                total_amount    = totalFee.ToString("0.00"),
                subject         = subject,
                body            = body,
                out_trade_no    = orderNo,
            };

            sParaTemp.Add("app_id", Config.AlipayConfig.AppId);

            string bizcont = SerializeMemoryHelper.SerializeToJson <AliPayBussiness>(bussin);

            sParaTemp.Add("biz_content", bizcont);
            sParaTemp.Add("charset", Config.Input_charset);
            sParaTemp.Add("method", "alipay.trade.app.pay");
            sParaTemp.Add("format", "json");
            sParaTemp.Add("sign_type", Config.AlipayConfig.Sign_type); //这个单拿出来,和sign拼到最后
            sParaTemp.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sParaTemp.Add("version", "1.0");
            sParaTemp.Add("notify_url", Config.AlipayConfig.Notify_url);
            //生成Url
            string content = sParaTemp.ToUrl();
            //生成签名
            string sign = RSAFromPkcs8.sign(content, Config.AlipayConfig.Private_key, Config.AlipayConfig.Input_charset);

            //追加签名
            content += "&sign=" + sign;
            //整个字符做UrlEncode
            content = RSAFromPkcs8.EncodeEx(content);
            Lind.DDD.Logger.LoggerFactory.Instance.Logger_Info("向APP返回的字符串包括Sign:" + content);
            return(content);
        }
Esempio n. 4
0
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var responseDataSegment = new ResponseDataSegment();

            var context = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"]; //获取传统context
            var Request = context.Request;                                                     //定义传统request对象


            var query = Request.QueryString["dataSeg"];
            RequestDataSegment data = new RequestDataSegment();

            if (query != null)
            {
                data = SerializeMemoryHelper.DeserializeFromJson <RequestDataSegment>(query);
            }

            if (data != null && data.RequestData.Any())
            {
                foreach (var item in data.RequestData)
                {
                    try
                    {
                        HttpResponseMessage response;
                        var handler = new HttpClientHandler()
                        {
                            AutomaticDecompression = DecompressionMethods.GZip
                        };
                        using (var http = new HttpClient(handler))
                        {
                            if (item.HttpMethod == 0)
                            {
                                if (item.RequestParam != null)
                                {
                                    item.Url += "?";
                                    foreach (var p in item.RequestParam)
                                    {
                                        item.Url += p.Key + "=" + p.Value + "&";
                                    }
                                    item.Url = item.Url.Remove(item.Url.Length - 1, 1);
                                }
                                response = http.GetAsync(item.Url).Result;
                            }
                            else
                            {
                                var content = new FormUrlEncodedContent(item.RequestParam);
                                response = http.PostAsync(item.Url, content).Result;
                            }

                            response.EnsureSuccessStatusCode();
                            responseDataSegment.ResponseData.Add(new ResponseMessage
                            {
                                GuidKey = item.GuidKey,
                                Status  = 200,
                                Result  = response.Content.ReadAsStringAsync().Result
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        responseDataSegment.ResponseData.Add(new ResponseMessage
                        {
                            GuidKey = item.GuidKey,
                            Status  = 100,
                            Result  = ex.Message
                        });
                    }
                }
            }
            actionContext.Response = new HttpResponseMessage {
                Content = new StringContent(SerializeMemoryHelper.SerializeToJson(responseDataSegment), Encoding.GetEncoding("UTF-8"), "application/json")
            };
            base.OnActionExecuting(actionContext);
        }
Esempio n. 5
0
        /// <summary>
        /// 方法拦截
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            #region 例外
            bool skipAuthorization = actionContext.ControllerContext.ControllerDescriptor.ControllerType.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) ||
                                     actionContext.ControllerContext.ControllerDescriptor.ControllerType.IsDefined(typeof(AllowAnonymousAttribute), inherit: true);

            if (skipAuthorization)
            {
                return;
            }
            #endregion

            #region 初始化
            var context  = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"]; //获取传统context
            var request  = context.Request;                                                     //定义传统request对象
            var paramStr = new StringBuilder();
            //校验码只从Url地址后取,不从表单取
            var coll = new NameValueCollection(request.QueryString);
            coll.Add(request.Form);

            //StringContent方式的提交
            //var task = actionContext.Request.Content.ReadAsByteArrayAsync().Result;
            //var result = Encoding.UTF8.GetString(task);
            //if (!string.IsNullOrWhiteSpace(result))
            //{
            //    foreach (var key in result.FromJson().Keys)
            //        coll.Add(key, (string)result.FromJson()[key]);
            //}


            #endregion

            #region 解析XML配置文件
            var config = CacheConfigFile.ConfigFactory.Instance.GetConfig <ApiValidateModelConfig>()
                         .ApiValidateModelList
                         .FirstOrDefault(i => i.AppKey == coll["AppKey"]);
            if (config == null)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(SerializeMemoryHelper.SerializeToJson(new ResponseMessage()
                    {
                        Status       = 0,
                        ErrorCode    = "001",
                        ErrorMessage = "AppKey不是合法的,请先去组织生成有效的Key"
                    }))
                };
                base.OnActionExecuting(actionContext);
                return;
            }
            if (config.ExpireDate < DateTime.Now)
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(SerializeMemoryHelper.SerializeToJson(new ResponseMessage()
                    {
                        Status       = 0,
                        ErrorCode    = "002",
                        ErrorMessage = "AppKey不是合法的,密钥已过期"
                    }))
                };
                base.OnActionExecuting(actionContext);
                return;
            }
            #endregion

            #region 验证算法

            var keys = new List <string>();
            foreach (string param in coll.Keys)
            {
                if (!string.IsNullOrEmpty(param))
                {
                    keys.Add(param.ToLower());
                }
            }
            keys.Sort();
            foreach (string p in keys)
            {
                if (p != ApiValidateHelper.CipherText)
                {
                    if (!string.IsNullOrEmpty(coll[p]))
                    {
                        paramStr.Append(coll[p]);
                    }
                }
            }
            paramStr.Append(config.PassKey);
            #endregion

            double timeStamp;
            if (double.TryParse(coll["timeStamp"], out timeStamp))
            {
                //当前UTC时间与1700/01/01的时间戳
                if (((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds - timeStamp) / 60 > config.ValidateMinutes)
                {
                    actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                    {
                        Content = new StringContent(SerializeMemoryHelper.SerializeToJson(new ResponseMessage()
                        {
                            Status       = 0,
                            ErrorCode    = "003",
                            ErrorMessage = "请求已过期"
                        }))
                    };
                }
            }
            else
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(SerializeMemoryHelper.SerializeToJson(new ResponseMessage()
                    {
                        Status       = 0,
                        ErrorCode    = "003",
                        ErrorMessage = "时间戳异常"
                    }))
                };
            }
            if (Lind.DDD.Utils.Encryptor.Utility.EncryptString(paramStr.ToString(), Lind.DDD.Utils.Encryptor.Utility.EncryptorType.MD5) != coll[ApiValidateHelper.CipherText])
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content = new StringContent(SerializeMemoryHelper.SerializeToJson(new ResponseMessage()
                    {
                        Status       = 0,
                        ErrorCode    = "003",
                        ErrorMessage = "验证失败,请求非法"
                    }))
                };
            }

            base.OnActionExecuting(actionContext);
        }
        public static void Append(this IDatabase cache, string key, object value)
        {
            string json = SerializeMemoryHelper.SerializeToJson(value);

            cache.StringAppend(key, json);
        }
        /// <summary>
        /// 设置键对应的值,过期时间后自己删除
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expireMinutes"></param>
        public static void Set(this IDatabase cache, string key, object value, int expireMinutes)
        {
            string json = SerializeMemoryHelper.SerializeToJson(value);

            cache.StringSet(key, json, TimeSpan.FromMinutes(expireMinutes));
        }
 /// <summary>
 /// 出队列JSON
 /// </summary>
 /// <param name="cache"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static void PushJson <T>(this IDatabase cache, string key, T obj)
 {
     cache.ListRightPush(key, SerializeMemoryHelper.SerializeToJson <T>(obj));
 }