void IExceptionFilter.OnException(ExceptionContext filterContext)
        {
            LogUtil.SetTrackId();

            bool   isAjaxRequest = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest();
            string controller    = (string)filterContext.RouteData.Values["controller"];
            string action        = (string)filterContext.RouteData.Values["action"];

            MessageBox messageBox = new MessageBox();

            messageBox.No      = Guid.NewGuid().ToString();
            messageBox.Type    = MessageBoxTip.Exception;
            messageBox.Title   = "抱歉,系统发生异常,如有需要请联系客服 " + OwnWebSettingUtils.GerServicePhone();
            messageBox.Content = "<a href=\"javascript:void(0)\" onclick=\"window.top.location.href='" + OwnWebSettingUtils.GetHomePage() + "'\">返回主页</a>";
            messageBox.IsTop   = true;
            if (CommonUtil.CanViewErrorStackTrace())
            {
                // messageBox.ErrorStackTrace = CommonUtils.ToHtml(filterContext.Exception.Message + "\r\n" + filterContext.Exception.StackTrace);
            }

            //判断是否异步调用
            if (isAjaxRequest)
            {
                CustomJsonResult jsonResult = new CustomJsonResult(ResultType.Exception, ResultCode.Exception, messageBox.Title, messageBox);
                //jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                filterContext.Result = jsonResult;
                filterContext.Result.ExecuteResult(filterContext);
                filterContext.HttpContext.Response.End();
            }
            else
            {
                string masterName = "_Layout";


                filterContext.Result = new ViewResult {
                    ViewName = "MessageBox", MasterName = masterName, ViewData = new ViewDataDictionary {
                        Model = messageBox
                    }
                };
            }


            filterContext.ExceptionHandled = true;

            log.Error("发生异常错误[编号:" + messageBox.No + "]", filterContext.Exception);
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);

            LogUtil.Info("当前未登录的URL:" + filterContext.HttpContext.Request.RawUrl);

            string userAgent = filterContext.HttpContext.Request.UserAgent;
            string loginPage = OwnWebSettingUtils.GetLoginPage("");

            if (userAgent.ToLower().Contains("micromessenger"))
            {
                LogUtil.Info("去往微信浏览器授权验证");
                loginPage = OwnWebSettingUtils.WxOauth2("");
            }
            else
            {
                LogUtil.Info("去往用户登录页面验证");
            }

            if (!filterContext.HttpContext.Request.IsAuthenticated)
            {
                LogUtil.Info("用户没有登录或登录超时");

                bool isAjaxRequest = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest();
                if (isAjaxRequest)
                {
                    MessageBox messageBox = new MessageBox();
                    messageBox.No        = Guid.NewGuid().ToString();
                    messageBox.Type      = MessageBoxTip.Exception;
                    messageBox.Title     = "您没有权限访问,可能链接超时,请登录";
                    messageBox.LoginPage = loginPage;
                    CustomJsonResult jsonResult = new CustomJsonResult(ResultType.Exception, ResultCode.Exception, "", messageBox);
                    filterContext.Result = jsonResult;
                    filterContext.Result.ExecuteResult(filterContext);
                    filterContext.HttpContext.Response.End();
                }
                else
                {
                    filterContext.Result = new RedirectResult(loginPage);
                }
            }
        }
예제 #3
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            LogUtil.SetTrackId();

            base.OnActionExecuting(filterContext);

            var request = filterContext.HttpContext.Request;

            bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), inherit: true);

            if (!skipAuthorization)
            {
                var userInfo = OwnRequest.GetUserInfo();

                bool isAjaxRequest = filterContext.RequestContext.HttpContext.Request.IsAjaxRequest();

                string userAgent = filterContext.HttpContext.Request.UserAgent;

                string returnUrl = "";

                if (isAjaxRequest)
                {
                    returnUrl = request.UrlReferrer.PathAndQuery;
                }
                else
                {
                    returnUrl = request.Url.PathAndQuery;
                }

                if (!string.IsNullOrEmpty(returnUrl))
                {
                    LogUtil.Info("OwnBaseController1->returnUrl:" + returnUrl);

                    returnUrl = System.Web.HttpUtility.UrlEncode(returnUrl);

                    LogUtil.Info("OwnBaseController2->returnUrl:" + returnUrl);
                }

                if (userInfo == null)
                {
                    LogUtil.Info("用户没有登录或登录超时");

                    string loginPage = OwnWebSettingUtils.GetLoginPage(returnUrl);

                    if (userAgent.ToLower().Contains("micromessenger"))
                    {
                        LogUtil.Info("去往微信浏览器授权验证");
                        loginPage = OwnWebSettingUtils.WxOauth2(returnUrl);
                    }
                    else
                    {
                        LogUtil.Info("去往用户登录页面验证");
                    }

                    if (isAjaxRequest)
                    {
                        MessageBox messageBox = new MessageBox();
                        messageBox.No        = Guid.NewGuid().ToString();
                        messageBox.Type      = MessageBoxTip.Failure;
                        messageBox.Title     = "请登录";
                        messageBox.LoginPage = loginPage;
                        CustomJsonResult jsonResult = new CustomJsonResult(ResultType.NoLogin, ResultCode.Failure, "", messageBox);
                        filterContext.Result = jsonResult;
                        filterContext.Result.ExecuteResult(filterContext);
                        filterContext.HttpContext.Response.End();
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult(loginPage);
                    }
                }
                else
                {
                    LogUtil.Info("用户Id:" + this.CurrentUserId);
                }
            }
        }