示例#1
0
        /// <summary>
        /// 处理请求,完成安全验证,调用接口方法
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            ApiRequestLog ar = new ApiRequestLog();

            context.Response.AddHeader("Access-Control-Allow-Origin", "*");

            //验证请求参数
            string                      errMsg             = string.Empty; //异常错误
            string                      signKeyToken       = string.Empty; //
            string                      postJson           = string.Empty; //json
            int                         apiType            = 0;            //0-XCloud项目,1-XCGame项目,2-xcgamemana项目
            ApiMethodAttribute          apiMethodAttribute = new ApiMethodAttribute();
            AuthorizeAttribute          authorizeAttribute = new AuthorizeAttribute();
            MethodInfo                  requestMethodInfo  = null;
            Dictionary <string, object> dicParas           = null;
            string                      requestUrl         = string.Empty;
            string                      action             = RequestHelper.GetString("action");

            try
            {
                //获取请求的方法信息

                GetMethodInfo(this, action, ref requestMethodInfo, ref apiMethodAttribute, ref authorizeAttribute);

                if (requestMethodInfo == null)
                {
                    isSignKeyReturn = IsSignKeyReturn(apiMethodAttribute.SignKeyEnum);
                    errMsg          = "请求方法无效";
                    FailResponseOutput(context, apiMethodAttribute, errMsg, signKeyToken);
                    return;
                }

                //验证请求参数
                if (!CheckRequestParam(context, apiMethodAttribute, ref dicParas, out errMsg, out postJson, out apiType, out requestUrl, out sysId, out versionNo))
                {
                    FailResponseOutput(context, apiMethodAttribute, errMsg, signKeyToken);
                    ar.show(apiType, requestUrl + "?action=" + action, postJson, Return_Code.F, errMsg, sysId);
                    return;
                }

                //验证参数签名
                if (!CheckSignKey(apiMethodAttribute.SignKeyEnum, dicParas, out signKeyToken, out errMsg))
                {
                    FailResponseOutput(context, apiMethodAttribute, errMsg, signKeyToken);
                    ar.show(apiType, requestUrl + "?action=" + action, postJson, Return_Code.F, errMsg, sysId);
                    return;
                }

                //验证访问权限
                if (!CheckAuthorize(authorizeAttribute, apiMethodAttribute.SignKeyEnum, dicParas, out errMsg))
                {
                    FailResponseOutput(context, apiMethodAttribute, errMsg, signKeyToken);
                    ar.show(apiType, requestUrl + "?action=" + action, postJson, Return_Code.F, errMsg, sysId);
                    return;
                }

                //验证是否锁定接口
                //if(!CheckIconOutputLock(apiMethodAttribute,dicParas,out errMsg))
                //{
                //    ar.show(apiType, requestUrl + "?action=" + action, postJson, Return_Code.F, errMsg, sysId);
                //    var obj = ResponseModelFactory.CreateModel(isSignKeyReturn, Return_Code.T, "", Result_Code.F, errMsg);
                //    SuccessResponseOutput(context, apiMethodAttribute, obj, signKeyToken);
                //    return;
                //}

                //调用请求方法
                object[] paras = null;
                if (requestMethodInfo.GetParameters().Count <object>() > 0)
                {
                    paras = new object[1] {
                        dicParas
                    };
                }
                object resObj = requestMethodInfo.Invoke(this, paras);
                SuccessResponseOutput(context, apiMethodAttribute, resObj, signKeyToken);


                string return_code;
                string return_msg;
                string result_code;
                string result_msg;
                GetResObjInfo(resObj, out return_code, out return_msg, out result_code, out result_msg);
                ar.show(apiType, requestUrl + "?action=" + action, postJson, return_code, return_msg, sysId, result_msg);
            }
            catch (Exception ex)
            {
                FailResponseOutput(context, apiMethodAttribute, ex.Message, signKeyToken);
                LogHelper.SaveLog(TxtLogType.Api, TxtLogContentType.Exception, TxtLogFileType.Day, Utils.GetException(ex));
                ar.show(apiType, requestUrl + "?action=" + action, postJson, Return_Code.F, Utils.GetException(ex), sysId);
            }
        }
示例#2
0
 public async Task <MessageModel> Add(ApiRequestLog model)
 {
     model.Id = 0;
     return(new MessageModel(await _ApiRequestLogServices.Add(model) > 0));
 }
示例#3
0
 public async Task <MessageModel> Update(ApiRequestLog model)
 {
     return(new MessageModel(await _ApiRequestLogServices.Update(model)));
 }
        public async Task InvokeAsync(HttpContext context)
        {
            if (Appsettings.app("Middleware", "RequestResponseLog", "Enabled").ToBool())
            {
                // 过滤,只有接口
                if (context.Request.Path.Value.Contains("api") && !context.Request.QueryString.ToString().Contains("Page"))
                {
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    context.Request.EnableBuffering();
                    Stream originalBody = context.Response.Body;
                    string Path         = context.Request.Path + context.Request.QueryString;
                    string DataRequest  = string.Empty;
                    string DataResponse = string.Empty;
                    try
                    {
                        DataRequest = await RequestData(context);

                        using (var ms = new MemoryStream())
                        {
                            context.Response.Body = ms;

                            await _next(context);

                            DataResponse = ResponseData(context.Response, ms);

                            ms.Position = 0;
                            await ms.CopyToAsync(originalBody);
                        }
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        context.Response.Body = originalBody;
                    }
                    stopwatch.Stop();
                    Parallel.For(0, 1, s =>
                    {
                        ApiRequestLog requestLog = new ApiRequestLog()
                        {
                            userName          = user.Name,
                            consumingTime     = stopwatch.ElapsedMilliseconds,
                            method            = context.Request.Method.ToLower(),
                            FormDataparameter = DataRequest,
                            path         = context.Request.Path,
                            Urlparameter = context.Request.QueryString.ToString(),
                            ResponseData = DataResponse,
                            state        = context.Response.StatusCode == StatusCodes.Status200OK ? Requeststate.succeed : Requeststate.error,
                            requestTime  = DateTime.Now
                        };
                        requestLogServices.Add(requestLog);
                    });
                }
                else
                {
                    await _next(context);
                }
            }
            else
            {
                await _next(context);
            }
        }