Example #1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            WebController defaultController = (WebController)context.Controller;

            if (defaultController.UserInfoSession == null)
            {
                return;
            }
            AppServiceFactory appServiceFactory = new AppServiceFactory(context.HttpContext.RequestServices);
            var permissionService = appServiceFactory.CreateService <IPermissionService>();

            var claims = permissionService.GetUnionPermission(defaultController.UserInfoSession);
            //页面无权限过滤
            var url  = $"/{context.RouteData.Values["Controller"].ToString()}/{context.RouteData.Values["Action"].ToString()}";
            var flag = claims.Where(x => x.Url != null).Where(x => x.Url.ToUpper() == url.ToUpper()).Count() > 0;

            if (!flag)
            {
                SesJsonResult jsonResult = new SesJsonResult(JsonResultStatus.Unauthorized, "无权限");
                context.Result = new ContentResult()
                {
                    Content = JsonConvert.SerializeObject(jsonResult)
                };
                return;
            }

            //按钮权限控制
            var btnPermission = permissionService.GetUnionBtnPermission(defaultController.UserInfoSession, url);
            Dictionary <string, string> btnPermissionDic = new Dictionary <string, string>();
            var style = "display:none;";

            foreach (var item in BtnPermission.AllBtnPms.Split(','))
            {
                if (!btnPermission.Contains(item))
                {
                    btnPermissionDic.Add(item, style);
                }
                else
                {
                    btnPermissionDic.Add(item, string.Empty);
                }
            }
            defaultController.ViewBag.BtnPermissionDic = btnPermissionDic;
            base.OnActionExecuting(context);
        }
        /// <summary>
        /// 进入控制器前账户进行权限认证,已经登出的账户,访问页面时跳转到登陆页面
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequest httpRequest    = filterContext.HttpContext.Request;
            var         controllerName = filterContext.RouteData.Values["Controller"].ToString();
            var         actionName     = filterContext.RouteData.Values["Action"].ToString();
            var         isLoginPath    = controllerName == "Account" && actionName == "Login";

            //如果已登陆 或 是登陆页面则跳过
            if (filterContext.HttpContext.User.Identity.IsAuthenticated || isLoginPath)
            {
                return;
            }

            //Http请求分为Ajax请求和普通请求
            if (httpRequest.isAjaxRequest())
            {
                SesJsonResult jsonResult    = new SesJsonResult(JsonResultStatus.NotLogin, "未登陆或登陆超时,请重新登陆");
                ContentResult contentResult = new ContentResult()
                {
                    Content = JsonConvert.SerializeObject(jsonResult)
                };
                filterContext.Result = contentResult;
                return;
            }
            else
            {
                var url = "/Account/Login";
                url = string.Concat(url, "?returnUrl=", httpRequest.Path);
                //跳转页面
                //RedirectResult redirectResult = new RedirectResult(url);
                //filterContext.Result = redirectResult;

                HttpContext.Response.WriteAsync("<script>window.parent.location='" + url + "'</script>");
                //filterContext.HttpContext.Response.WriteAsync("<script>window.parent.location.href=" + url + "</script>");
                return;
            }
        }