public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Exception ex = actionExecutedContext.Exception;

            LogicalThreadContext.Properties["trackid"] = Guid.NewGuid().ToString();
            log.Error("API调用出现异常", ex);

            OwnApiHttpResult result = new OwnApiHttpResult(ResultType.Exception, ResultCode.Exception, "程序发生异常");

            actionExecutedContext.Response = new OwnApiHttpResponse(result);

            base.OnException(actionExecutedContext);
        }
Exemple #2
0
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            try
            {
                DateTime requestTime   = DateTime.Now;
                var      request       = ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request;
                var      requestMethod = request.HttpMethod;

                bool skipAuthorization = actionContext.ActionDescriptor.GetCustomAttributes <AllowAnonymousAttribute>().Any();
                if (skipAuthorization)
                {
                    return;
                }

                var token = request.QueryString["token"];
                if (token == null)
                {
                    token = request.Headers["X-Token"];
                }

                if (string.IsNullOrEmpty(token))
                {
                    OwnApiHttpResult result = new OwnApiHttpResult(ResultType.Failure, ResultCode.NeedLogin, "token不能为空");
                    actionContext.Response = new OwnApiHttpResponse(result);
                    base.OnActionExecuting(actionContext);
                    return;
                }

                var tokenInfo = SSOUtil.GetTokenInfo(token);

                if (tokenInfo == null)
                {
                    OwnApiHttpResult result = new OwnApiHttpResult(ResultType.Failure, ResultCode.NeedLogin, "token 已经超时");
                    actionContext.Response = new OwnApiHttpResponse(result);
                    return;
                }

                base.OnActionExecuting(actionContext);
            }
            catch (Exception ex)
            {
                LogUtil.Error(string.Format("API错误:{0}", ex.Message), ex);
                OwnApiHttpResult result = new OwnApiHttpResult(ResultType.Exception, ResultCode.Exception, "内部错误");
                actionContext.Response = new OwnApiHttpResponse(result);
                return;
            }
        }
Exemple #3
0
 public OwnApiHttpResponse ResponseResult(OwnApiHttpResult result)
 {
     return(new OwnApiHttpResponse(result));
 }