Ejemplo n.º 1
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var api         = context.ActionDescriptor.AttributeRouteInfo.Template;
            var isEnableLog = IsEnableLog(context);

            try
            {
                // 设置当前访问信息
                string clientInfo = context.HttpContext.Connection?.RemoteIpAddress.ToString();
                string requestId  = context.HttpContext.Request.Headers["requestId"];
                if (string.IsNullOrEmpty(requestId))
                {
                    requestId = context.HttpContext.TraceIdentifier;
                    context.HttpContext.Request.Headers["requestId"] = requestId;
                }
                app.ActionArguments          = context.ActionArguments;
                AppManager.CurrentAppContext = app;

                // 记录日志
                if (isEnableLog)
                {
                    string parm = JsonConvert.SerializeObject(app.ActionArguments);
                    app.LogRequest(requestId, context.ActionDescriptor.DisplayName, parm, clientInfo, context.ActionDescriptor.AttributeRouteInfo.Template);
                }

                // 读取或设置sessionId和language
                if (IsEnableCookie(context))
                {
                    InitCookies(api);
                }

                // 访问限制,登录等方法不受限制
                if (AuthenticationProxy.IsCheckGateway(context))
                {
                    var clientIp = context.HttpContext.Connection?.RemoteIpAddress.ToString();
                    if (!string.IsNullOrEmpty(clientIp))
                    {
                        GatewayManager.Check(clientIp);
                    }
                }

                var authentication = app.GetService <IAuthenticationProxy>();

                // 鉴权-检查用户token,设置当前用户上下文
                var token = (context.HttpContext.Request.Headers[ApiDocManager.TokenHeaderName]).ToString().Replace("Bearer ", "");
                if (AuthenticationProxy.IsCheckToken(context) || !string.IsNullOrEmpty(token))
                {
                    if (ConfigManager.Configuration["EnableApiDoc"] == "1" && string.IsNullOrEmpty(token))
                    {
                        token = ConfigManager.Configuration["DefaultToken"];
                    }
                    authentication.SetCurrentUser(token, context);
                }
                authentication.SetCurrentGuest(app.SessionID);

                // 检查数据
                VaildateModel(context);

                // 异步
                var asyncTask = AsyncTaskManager.GetTaskInfo(context, app.User?.UserID.ToString());
                if (asyncTask != null)
                {
                    var resultValue = ResultBuilder.AsSuccess(asyncTask);
                    context.Result = new JsonResult(resultValue);
                    return;
                }

                // 从缓存返回
                var cacheResult = CacheManager.ReadCache(context, app);
                if (cacheResult != null)
                {
                    cacheResult.RequestId = context.HttpContext.TraceIdentifier;
                    var cacheTimeRate = CacheTimeSettings.GetCacheTimeRate();
                    var cacheSeconds  = CacheManager.CalcCacheSeconds(context.ActionDescriptor as ControllerActionDescriptor, out var cacheTimeSetting);
                    var seconds       = cacheTimeRate.HttpCacheTime * cacheSeconds;
                    if (seconds < 10)
                    {
                        seconds = 10;
                    }
                    if (seconds > 60)
                    {
                        seconds = 60;
                    }

                    context.HttpContext.Response.GetTypedHeaders().CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
                    {
                        Public = true,
                        MaxAge = TimeSpan.FromSeconds(seconds)
                    };
                    context.Result = new JsonResult(cacheResult);
                    return;
                }
            }
            // 不捕获异常,由ExceptionFilter处理。
            finally
            {
                if (context.Result != null)
                {
                    CleanOnActionClose();
                    if (isEnableLog)
                    {
                        app.LogResponseResult(context.Result);
                        app.EndRequest();
                    }
                }
            }
        }