コード例 #1
0
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var ignore = IgnoreVerify(context);

            if (!ignore)
            {
                var request = context.HttpContext.Request;
                _cache  = context.HttpContext.RequestServices.GetService(typeof(IMemoryCache)) as IMemoryCache;
                _logger = context.HttpContext.RequestServices.GetService(typeof(ILogger <AntiReplayFilter>)) as ILogger <AntiReplayFilter>;

                var verifyHeader = AntiReplayHelper.VerifyHeader(request, _cache);
                if (!verifyHeader)
                {
                    ResponseHandle(context, "Incorrect request header");
                    return;
                }

                var dataDic = await AntiReplayHelper.GetRequestData(request);

                var dataStr = AntiReplayHelper.DicToString(dataDic);
                var encode  = HttpUtility.UrlEncode(dataStr)?.ToLower().Replace("+", "%20");
                var sign    = SecretHelper.Md5(encode);
                _logger.LogInformation($"request json data:{encode}, generate sign:{sign}, request sign:{request.Headers["X-CA-SIGNATURE"].ToString()}");
                if (!sign.Equals(request.Headers["X-CA-SIGNATURE"].ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    ResponseHandle(context, "Incorrect signature");
                    return;
                }
            }

            await base.OnActionExecutionAsync(context, next);
        }