Beispiel #1
0
        /// <summary>
        /// 验证权限
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public virtual VerificationEntity Verify(ApiArgsEntity args)
        {
            if (string.IsNullOrWhiteSpace(args.Token) || string.IsNullOrWhiteSpace(args.Method) || string.IsNullOrWhiteSpace(args.Ip))
            {
                return(null);
            }
            var info = VerifyVoucher(args);

            if (info != null && info.Error == null)
            {
                bool isSign = true;
                if (info.VoucherProtocol != null)
                {
                    isSign = info.VoucherProtocol.IsSign;
                }
                else if (info.Voucher != null)
                {
                    isSign = info.Voucher.IsSign;
                }
                else if (info.Protocol != null)
                {
                    isSign = info.Protocol.IsSign;
                }
                if (isSign)
                {
                    VerifySign(info, args.Token, args.Timestamp, args.Value, args.Sign);
                }
            }

            return(info);
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="filterContext"></param>
        protected virtual bool Verify(ActionExecutingContext filterContext)
        {
            var value = GetValue(filterContext);
            var sign  = filterContext.RequestContext.RouteData.Values["sign"] ??
                        filterContext.RequestContext.HttpContext.Request["sign"];
            var token = filterContext.RequestContext.RouteData.Values["token"] ??
                        filterContext.RequestContext.HttpContext.Request["token"];
            var args = new ApiArgsEntity
            {
                Ip     = HttpContextHelper.GetClientIp(),
                Method = Method,
                Sign   = sign.Convert <string>(),
                Token  = token.Convert <string>(),
                Value  = value
            };
            var info = Ioc.Resolve <IApiEngineApplicationService>().Verify(args);

            filterContext.Controller.ViewBag.Verification = info;
            filterContext.Controller.ViewBag.Method       = Method;
            if (info != null && info.Error != null)
            {
                filterContext.Result = new ContentResult {
                    Content = GetErrorResult(info.Error.Key, info.Error.Message)
                };
            }
            return(info != null && info.IsPass);
        }
Beispiel #3
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);
        }