public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 如果未登录,则跳转到登录界面
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.HttpContext.Response.Redirect("/Account/LogOn");
                return;
            }
            //当前登录用户的用户名
            string userName = filterContext.HttpContext.User.Identity.Name;
            //当前登录用户对象
            User user = SampleData.users.Find(u => u.UserName == userName);

            if (user != null)
            {
                //当前登录用户的角色
                Role role = SampleData.roles.Find(r => r.Id == user.RoleId);

                //获得controller:
                string controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower();
                if (ActionName == null)
                {
                    ActionName = filterContext.RouteData.Values["action"].ToString();
                }


                //查询角色id
                RoleWithControllerAction roleWithControllerAction = SampleData.roleWithControllerAndAction.Find(r => r.ControllerName.ToLower() == controllerName && ActionName.ToLower() == ActionName.ToLower());
                if (roleWithControllerAction != null)
                {
                    //有权限操作当前控制器和Action的角色id
                    this.Roles = roleWithControllerAction.RoleIds;
                }
                if (!string.IsNullOrEmpty(Roles))
                {
                    foreach (string roleid in Roles.Split(','))
                    {
                        if (role.Id.ToString() == roleid)
                        {
                            //return就说明有权限了,后面的代码就不跑了,直接返回视图给浏览器就好
                            return;
                        }
                    }
                }

                filterContext.Result = new ViewResult {
                    ViewName = "Error",
                };
                return;
            }
            else
            {
                filterContext.Result = new EmptyResult();
                filterContext.HttpContext.Response.Redirect("/Account/Logon", true);
                return;
            }
        }
Exemple #2
0
        }                                          //可以操作当前Action的角色Id集合

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string userName = filterContext.HttpContext.User.Identity.Name;
            User   user     = SampleData.users.Find(u => u.UserName == userName);

            if (user != null)
            {
                string controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower();
                string actionName     = filterContext.RouteData.Values["action"].ToString().ToLower();
                if (ActionName == null)
                {
                    ActionName = actionName;
                }

                RoleWithControllerAction roleWithControllerAction = SampleData.roleWithControllerAndAction.Find(r => r.ControllerName.ToLower() == controllerName &&
                                                                                                                controllerName.ToLower() == ActionName.ToLower());
                if (roleWithControllerAction != null)
                {
                    this.Roles = roleWithControllerAction.RoleIds;                         //有权限操作当前控制器和Action的角色id
                }
                if (!string.IsNullOrEmpty(Roles))
                {
                    Role role = SampleData.roles.Find(r => r.Id == user.RoleId);
                    foreach (string roleid in Roles.Split(','))
                    {
                        if (role.Id.ToString() == roleid)
                        {
                            return;                               //return就说明有权限
                        }
                    }
                }
                filterContext.Result = new EmptyResult();                   //请求失败输出空结果
                HttpContext.Current.Response.Write("对不起,你没有权限!");           //打出提示文字
                //return;
            }
            else
            {
                filterContext.Result = new EmptyResult();
                HttpContext.Current.Response.Write("对不起,请先登录!");
            }
            //base.OnActionExecuting(filterContext);
        }
Exemple #3
0
        /// <summary>
        /// 请求授权时执行
        /// </summary>
        /// <param name="httpContext"></param>
        /// <returns></returns>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //获取url请求中的controller和action
            string controllerName = filterContext.RouteData.Values["controller"].ToString().ToLower();
            string actionName     = filterContext.RouteData.Values["action"].ToString().ToLower();

            //获取用以下方式
            controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            actionName     = filterContext.ActionDescriptor.ActionName;

            //根据请求的controller和action去查询可以被哪些角色操作
            RoleWithControllerAction roleWithControllerAction = SampleData.roleWithControllerAndAction.Find(r => r.ControllerName.ToLower() == controllerName.ToLower() && actionName.ToLower() == actionName.ToLower());

            if (roleWithControllerAction != null)
            {
                this.Roles = roleWithControllerAction.RoleIds;
            }

            base.OnAuthorization(filterContext);
        }