Ejemplo n.º 1
0
        /// <summary>
        /// 验证请求限制
        /// </summary>
        /// <param name="info"></param>
        /// <param name="dayCount"></param>
        /// <param name="token"></param>
        /// <param name="protocolName"></param>
        /// <returns></returns>
        protected virtual void ValidateDayRequest(VerificationEntity info, long dayCount, string token, string protocolName)
        {
            if (dayCount == 0)
            {
                return;
            }
            var name  = string.Format("{0}{1}DayCount", token, protocolName);
            var value = CacheRepository.Get <Dictionary <string, object> >(name);
            var count = value == null || !value.ContainsKey("Count") ? 0 : value["Count"].Convert <long>();

            count++;
            if (count > dayCount)
            {
                info.SetError("00004", dayCount);
            }
            if (value == null)
            {
                value = new Dictionary <string, object>
                {
                    { "Count", count },
                    { "Time", DateTime.Now.Date.AddDays(1) },
                };
            }
            value["Count"] = count;
            CacheRepository.Set(name, value, value["Time"].Convert <DateTime>());
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 验证
 /// </summary>
 /// <param name="value"></param>
 /// <param name="info"></param>
 /// <param name="token"></param>
 /// <param name="sign"></param>
 /// <returns></returns>
 public virtual void VerifySign(VerificationEntity info, string token, string value, string sign)
 {
     if (string.IsNullOrEmpty(sign) || string.IsNullOrEmpty(token) || GetSign(token, value).ToLower() != sign.ToLower())
     {
         info.SetError("00002");
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 验证
 /// </summary>
 /// <param name="timestamp"></param>
 /// <param name="value"></param>
 /// <param name="info"></param>
 /// <param name="token"></param>
 /// <param name="sign"></param>
 /// <returns></returns>
 public virtual void VerifySign(VerificationEntity info, string token, string timestamp, string value, string sign)
 {
     if (Math.Abs((DateTime.Now - new DateTime(1700, 01, 01)).TotalSeconds - timestamp.Convert <long>()) > 600)
     {
         info.SetError("00002");
     }
     if (string.IsNullOrEmpty(sign) || string.IsNullOrEmpty(token) || GetSign(token, timestamp, value).ToLower() != sign.ToLower())
     {
         info.SetError("00002");
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 验证凭证
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        protected virtual VerificationEntity VerifyVoucher(ApiArgsEntity args)
        {
            var enginArgs = GetEngin();

            args.Method = args.Method?.ToLower();
            var info = new VerificationEntity
            {
                Voucher         = enginArgs.GetVoucher(args.Token),
                Protocol        = enginArgs.GetProtocol(args.Method),
                VoucherProtocol = enginArgs.GetVoucherProtocol(args.Token, args.Method)
            };

            if (info.Voucher == null || info.Voucher.Type == VoucherType.Forbid)
            {
                info.SetError("00002");
                return(info);
            }
            if (info.Voucher.Type == VoucherType.Global)
            {
                if (info.Protocol != null)
                {
                    ValidateRequest(info, info.Protocol.SecondCount, info.Protocol.DayCount, args.Token, args.Method);
                }
                return(info);
            }
            if (info.Protocol != null)
            {
                if (!info.Protocol.IsStart)
                {
                    info.SetError("00002");
                    return(info);
                }
                if (!info.Protocol.IsVerify)
                {
                    ValidateRequest(info, info.Protocol.SecondCount, info.Protocol.DayCount, args.Token, args.Method);
                    return(info);
                }
                if (info.VoucherProtocol == null || info.VoucherProtocol.IsForbid)
                {
                    info.SetError("00002");
                    return(info);
                }
                ValidateRequest(info, info.VoucherProtocol.SecondCount, info.VoucherProtocol.DayCount, args.Token, args.Method);
            }
            if (info.Voucher.IpsArray != null && !info.Voucher.IpsArray.ContainsKey(args.Ip))
            {
                info.SetError("00005");
                return(info);
            }
            return(info);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 验证请求限制
 /// </summary>
 /// <param name="info"></param>
 /// <param name="secondCount"></param>
 /// <param name="dayCount"></param>
 /// <param name="token"></param>
 /// <param name="protocolName"></param>
 /// <returns></returns>
 protected virtual void ValidateRequest(VerificationEntity info, int secondCount, long dayCount, string token,
                                        string protocolName)
 {
     ValidateSecondRequest(info, secondCount, token, protocolName);
     ValidateDayRequest(info, dayCount, token, protocolName);
 }