/// <summary> /// Action执行之前执行 /// </summary> /// <param name="filterContext">过滤器上下文</param> public void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.RequestContext.HttpContext.Request; try { //若为本地测试,则不需要登录 if (GlobalSwitch.RunModel == RunModel.LocalTest) { return; } //判断是否需要登录 bool needLogin = filterContext.ContainsAttribute <CheckLoginAttribute>() && !filterContext.ContainsAttribute <IgnoreLoginAttribute>(); //转到登录 if (needLogin && !Operator.Logged()) { RedirectToLogin(); } else { return; } } catch (Exception ex) { BusHelper.HandleException(ex); RedirectToLogin(); } void RedirectToLogin() { if (request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new AjaxResult { Success = false, ErrorCode = 1, Msg = "未登录" }.ToJson(), ContentEncoding = Encoding.UTF8, ContentType = "application/json" }; } else { UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext); string loginUrl = urlHelper.Content("~/Home/Login"); string script = $@" <html> <script> top.location.href = '{loginUrl}'; </script> </html> "; filterContext.Result = new ContentResult { Content = script, ContentType = "text/html", ContentEncoding = Encoding.UTF8 }; } } }
/// <summary> /// Action执行完毕之前执行 /// </summary> /// <param name="filterContext"></param> public void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.HttpContext.Request; try { //判断是否需要登录 List <string> attrList = FilterHelper.GetFilterList(filterContext); bool needLogin = attrList.Contains(typeof(CheckLoginAttribute).FullName) && !attrList.Contains(typeof(IgnoreLoginAttribute).FullName); //转到登录 if (needLogin && !Operator.Logged()) { RedirectToLogin(); } } catch (Exception ex) { BusHelper.HandleException(ex); RedirectToLogin(); } void RedirectToLogin() { filterContext.Result = new ContentResult { Content = new AjaxResult { Success = false, Code = 401, Msg = "未登录" }.ToJson(), ContentType = "application/json;charset=UTF-8" }; } }
/// <summary> /// Action执行之前执行 /// </summary> /// <param name="filterContext">过滤器上下文</param> public void OnActionExecuting(ActionExecutingContext filterContext) { var request = filterContext.HttpContext.Request; try { //若为本地测试,则不需要登录 if (GlobalSwitch.RunModel == RunModel.LocalTest) { return; } //判断是否需要登录 List <string> attrList = FilterHelper.GetFilterList(filterContext); bool needLogin = attrList.Contains(typeof(CheckLoginAttribute).FullName) && !attrList.Contains(typeof(IgnoreLoginAttribute).FullName); //转到登录 if (needLogin && !Operator.Logged()) { RedirectToLogin(); } } catch (Exception ex) { BusHelper.HandleException(ex); RedirectToLogin(); } void RedirectToLogin() { if (request.IsAjaxRequest()) { filterContext.Result = new ContentResult { Content = new AjaxResult { Success = false, Code = 401, Msg = "未登录" }.ToJson(), ContentType = "application/json;charset=UTF-8" }; } else { UrlHelper urlHelper = new UrlHelper(filterContext); string loginUrl = urlHelper.Content("~/Admin/Home/Login"); string script = $@" <html> <script> top.location.href = '{loginUrl}'; </script> </html> "; filterContext.Result = new ContentResult { Content = script, ContentType = "text/html" }; } } }
public void OnException(ExceptionContext context) { var ex = context.Exception; BusHelper.HandleException(ex); context.Result = new ContentResult { Content = new AjaxResult { Success = false, Msg = ex.Message }.ToJson() }; }
/// <summary> /// Action执行之前执行 /// </summary> /// <param name="filterContext">过滤器上下文</param> public void OnActionExecuting(ActionExecutingContext filterContext) { try { //若为本地测试,则不需要登录 if (GlobalSwitch.RunModel == RunModel.LocalTest) { return; } //判断是否需要登录 List <string> attrList = FilterHelper.GetFilterList(filterContext); bool needLogin = attrList.Contains(typeof(CheckLoginAttribute).FullName) && !attrList.Contains(typeof(IgnoreLoginAttribute).FullName); //转到登录 if (needLogin && !Operator.Logged()) { RedirectToLogin(); } } catch (Exception ex) { BusHelper.HandleException(ex); RedirectToLogin(); } void RedirectToLogin() { UrlHelper urlHelper = new UrlHelper(filterContext); string loginUrl = urlHelper.Content("~/Home/Login"); string script = $@" <html> <script> top.location.href = '{loginUrl}'; </script> </html> "; filterContext.Result = new ContentResult { Content = script, ContentType = "text/html" }; } }
/// <summary> /// 处理系统错误 /// </summary> /// <param name="exContext">错误消息</param> public override void OnException(ExceptionContext exContext) { base.OnException(exContext); exContext.ExceptionHandled = true; exContext.HttpContext.Response.StatusCode = 200; var theEx = exContext.Exception; BusHelper.HandleException(theEx); AjaxResult res = new AjaxResult() { Success = false, Msg = theEx.Message }; exContext.Result = new ContentResult() { Content = res.ToJson(), ContentEncoding = Encoding.UTF8 }; }