ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)(6)

public PermissionAttribute(params EnumMoudle[] parameters)
        {
            Permissions = parameters.ToList();
        }

public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //throw new NotImplementedException();
        }

public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //throw new NotImplementedException();
        }
    }
}

8.然后在Controller或者Action方法加上验证

复制代码 代码如下:


 [Permission(EnumMoudle.Employees),Authorize, ValidateInput(false)]
 [Permission(EnumMoudle.SysUserManage_Role)]

9.在用户管理Controller中添加权限分配,修改方法

复制代码 代码如下:


        #region 添加管理员
        /// <summary>
        /// 添加页
        /// </summary>
        /// <param>管理员实体类</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Add()
        {
            var moudleList = EnumHelper.GetItemValueList<EnumMoudle>();
            this.ViewBag.MoudleList = new SelectList(mouldeList, "Key", "Value");
            return View();
        }
        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param>实体类</param>
        /// <param></param>
        /// <returns></returns>
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(SystemUser model, FormCollection fc)
        {
            model.BusinessPermissionString = fc["MoudelList"];
            model.State = 1;
            model.CreateTime = DateTime.Now;
            systemuserrepository.SaveOrEditModel(model);
            return RedirectToAction("UserList");
        }
        #endregion
        //修改权限
        [Authorize, AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection fc)
        {
            var model = systemuserrepository.GetModel(id);
            if (model != null)
            {
                string password = model.PassWord;
                if (Request.Form["PassWord"] != "")
                {
                    model.BusinessPermissionString = fc["MoudleList"];
                    UpdateModel(model);
                    systemuserrepository.SaveOrEditModel(model);
                }
                else
                {
                    model.BusinessPermissionString = fc["MoudleList"];
                    UpdateModel(model);
                    model.PassWord = password;
                    systemuserrepository.SaveOrEditModel(model);
                }
                return RedirectToAction("userlist");
            }
            else
                return View("404");
        }
        #endregion

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzwdd.html