设计方向:

1.摒弃SiteMap,避免在容易书写错误的sitemap中书写,导航在controller和action上打标签生成。

2.controller统一继承basecontroller,在basecontroller上统一权限拦截。

3.使用缓存保存权限列表,避免反复读取数据库。

4.使用Elinq作为ORM工具

5.使用页面部分试图权限

一. 获取权限视图缓存列表

        /// <summary>
        /// 获取权限视图缓存列表
        /// </summary>
        /// <returns></returns>
        public static List<AuthModel> GetAuthCache()
        {
            List<AuthModel> viewRoleGroup;
            string key = "ViewAuthList";
            if (HttpRuntime.Cache[key] != null)
            {
                viewRoleGroup = (List<AuthModel>)HttpRuntime.Cache[key];
            }
            else
            {
                viewRoleGroup = ElinqHelper.List<AuthModel>().Where(p => p.ID > ).ToList();
            }
            return viewRoleGroup; }

二.权限判断业务逻辑

   //权限判断业务逻辑
        protected virtual bool AuthorizeCore(ActionExecutingContext filterContext, bool isViewPage)
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("userCookie");
            if (cookie != null)
            {
                HttpContext.Current.Session["name"] = cookie.Values["name"];
                HttpContext.Current.Session["zu"] = cookie.Values["zu"];
            }             if (HttpContext.Current.Session["name"] == null) return false;
            if (HttpContext.Current.Session["name"].ToString() == "admin") return true;
            if (filterContext.HttpContext == null) throw new ArgumentNullException("httpContext");
            var controllerName = filterContext.RouteData.Values["controller"].ToString();
            var actionName = filterContext.RouteData.Values["action"].ToString();             var list = GetCache.GetAuthCache();
            var grouplist = list.FirstOrDefault(p => p.CONTROLLERNAME == controllerName && p.ACTIONNAME == actionName).GROUPISLIST;
            var group = Convert.ToDecimal(HttpContext.Current.Session["zu"]);
            if (grouplist.Contains(group))return true;
            return false; }

三.权限拦截

    [newAuthorizeFilterAttribute]
    public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
            filterContext.Result = new ViewResult()
            {
                ViewName = "Error",
                ViewData = filterContext.Controller.ViewData,
            };
            filterContext.ExceptionHandled = true;
        } } 

四.更新权限列表

        [Description("更新权限列表")]
        public ActionResult UpAuthList()
        {
            var count = ;
            var result = GetActionPermission.GetAllActionByAssembly().Where(p => !(p.ActionMethod is HttpPostAttribute));
            var dbConfiguration = ElinqHelper.dbConfiguration;
            ElinqHelper.AddClass<AuthModel>();
            using (var ctx = dbConfiguration.CreateDbContext())
            {
                var q = ctx.Set<AuthModel>();
                var sqlResult = q.Where(p => p.ID > );
                foreach (var item in result)
                {
                    var newId = ElinqHelper.NextVal("SQ_AUTH");
                    var auth = sqlResult.FirstOrDefault(p => p.CONTROLLERNAME == item.ControllerName
                        && p.ACTIONNAME == item.ActionName);
                    if (auth != null)
                    {
                        if (string.IsNullOrEmpty(auth.CONTROLLERNAME) || string.IsNullOrEmpty(auth.ACTIONNAME)) break;
                        if (auth.DESCRIPTION != item.Description)
                        {
                            if (q.Update(new { DESCRIPTION = item.Description, LASTTIME = DateTime.Now }, p => p.ID == auth.ID) > ) count++;
                        }
                    }
                    else
                    {
                        AuthModel model = new AuthModel
                        {
                            ID = newId,
                            CONTROLLERNAME = item.ControllerName,
                            ACTIONNAME = item.ActionName,
                            DESCRIPTION = item.Description,
                            LASTTIME = DateTime.Now
                        };
                        if (q.Insert(model) > )
                        {
                            newId++;
                            count++;
                        }
                    }
                }
            }
            return Content("成功更新" + count.ToString() + "个");}

五.导航

public static MvcHtmlString GetWebPath(this HtmlHelper html, string itemTemplate)
        {
            var sb = new StringBuilder();
            var controllerList = GetCache.GetControllerCache();
            var menu = GetCache.GetActionCache()
              .Where(p => p.Attrs.Count(q => q.TypeId.ToString() == "MvcApplication.Controllers.Menu") > );             CurrentUser currentuser = new CurrentUser();
            if (currentuser.ActionPermission == null)
                return MvcHtmlString.Create(sb.ToString());             foreach (var controller in controllerList)
            {
                if (currentuser.ActionPermission != null)
                {
                    var list = currentuser.ActionPermission.Where(p => p.CONTROLLERNAME == controller.ControllerName && p.CONTROLLERNAME != "Home");
                    foreach (var item in list)
                    {
                        var result = menu.FirstOrDefault(p => p.ActionName == item.ACTIONNAME && p.ControllerName == item.CONTROLLERNAME);
                        if (result != null)
                        {
                            sb.Append(itemTemplate.Replace("{0}", result.ControllerName).Replace("{1}", result.ActionName).Replace("{2}",
                                controller.Description));
                            break;
                        }
                    }
                }
            }
            return MvcHtmlString.Create(sb.ToString());}

六.页面代码块级权限设计

public static HelperResult RoleHtmlTags(this HtmlHelper htmlHelper, string action, Func<string, HelperResult> template)        {            var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();            CurrentUser currentuser = new CurrentUser();            var auth = currentuser.ActionPermission.Count(p => p.CONTROLLERNAME == controller && p.ACTIONNAME == action);            if (auth > 0)            {                return new HelperResult(writer =>                {                    writer.Write(template.Invoke(null));                });            }            else            {                return null;            }        }

页面使用方法

@Html.RoleHtmlTags("WorkLogEdit", @<span>@Html.ActionLink("编辑", "WorkLogEdit", new { id = dt.id })<span>|</span></span>) 

MVC权限模块的更多相关文章

  1. Web应用程序系统的多用户权限控制设计及实现-权限模块【10】

    前五章均是从整体上讲述了Web应用程序的多用户权限控制实现流程,本章讲述Web权限管理系统的权限配置模块.页面模块涉及到的数据表为权限表.权限配置模块是按照用户组和页面,栏目结合组成的.通过配置一个用 ...

  2. 开篇ASP.NET MVC 权限管理系列

    开篇 [快乐编程系列之ASP.NET MVC 权限管理系列]一.开篇   用了好长一段时间的ASP.NET MVC,感觉MVC真的是很好用,最近一年左右做了两个中小型项目,觉得还是很多地方不是很熟悉的 ...

  3. asp.net core权限模块的快速构建

    大部分系统都会有权限模块,别人家系统的权限怎么生成的我不知道,我只知道这样做是可以并且挺好的. 文章中只对asp.net core的部分代码进行说明 呃 记录~,mvc版本自行前往仓库查阅 代码中的一 ...

  4. 解析大型.NET ERP系统 权限模块设计与实现

    权限模块是ERP系统的核心模块之一,完善的权限控制机制给系统增色不少.总结我接触过的权限模块,以享读者. 1 权限的简明定义 ERP权限管理用一句简单的话来说就是:谁 能否 做 那些 事. 文句 含义 ...

  5. 从零开始编写自己的C#框架(18)——Web层后端权限模块——菜单管理

    从本章开始,主要讲解的是页面中对框架相关功能的调用方法,比如列表页面(又分为有层次感列表和普通列表).编辑页面.多标签页面等,只要熟悉了这些函数的使用方法,那么开发起来就会很便捷了. 1.如图先创建菜 ...

  6. nopcommerce之权限模块

    这篇文章简单介绍一下nopcommerce的权限模块,nopcommerce里面的权限设计相对比较简单,主要针对后台的action和前台的是否显示(比如产品.品牌等),虽然简单但是应付一般的项目应该没 ...

  7. mvc 权限管理 demo

    http://blog.csdn.net/zht666/article/details/8529646 new http://www.cnblogs.com/fengxing/archive/2012 ...

  8. Extjs4.1.x使用Application动态按需加载MVC各模块

    我们知道Extjs4之后提出了MVC模块开发,将以前肥厚的js文件拆分成小的js模块[model\view\controller\store\form\data等],通过controller拼接黏合, ...

  9. NET MVC异常处理模块

    一个简单的ASP.NET MVC异常处理模块   一.前言 异常处理是每个系统必不可少的一个重要部分,它可以让我们的程序在发生错误时友好地提示.记录错误信息,更重要的是不破坏正常的数据和影响系统运行. ...

随机推荐

  1. ORA-65096: invalid common user or role name

    CREATE USER xx IDENTIFIED BY yy DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP PROFILE "DEF ...

  2. SLua 中继承 C# 类接口 Slua.Class 的一个 Bug。

    由于目前要把大量的代码移植到 lua 中(真是够虐心的),面向对象肯定少不了,项目的代码都是这么设计的,于是就测试 Slua.Class 接口来扩展 C# 的类,发现有点问题,给作者提交了一个 Iss ...

  3. MFC如何获取编辑框中输入的内容

    1.GetDlgItemText() 2.先用 GetDlgItem(编辑框的ID)获取指向编辑框的指针.再用GetWindowText函数将获取内容保存至指定的字符数组里. 3.使用 GetDlgI ...

  4. uva 10047 the monocyle (四维bfs)

    算法指南白书 维护一个四维数组,走一步更新一步 #include<cstdio> #include<cstring> #include<queue> #includ ...

  5. ubuntu adb devices 找不到任何东西,安装驱动

    在Android平台下做开发,adb总是需要使用到的,同时,因为linux没有windows这样操作傻瓜化,有些东西还是需要自行设置的,否则将会连接不上. 关于这些内容,google也有一定的描述,可 ...

  6. Hadoop之Pig安装

    Pig可以看做是Hadoop的客户端软件,使用Pig Latin语言可以实现排序.过滤.求和.分组等操作. Pig的安装步骤: 一.去Pig的官方网站下载.http://pig.apache.org/ ...

  7. Java学习之路(一) —— Java命名规范

    Package 的命名 Package 的名字应该都是由一个小写单词组成. Class 的命名 Class 的名字必须由大写字母开头而其他字母都小写的单词组成 Class 变量的命名 变量的名字必须用 ...

  8. 3 weekend110的hadoop中的RPC框架实现机制 + hadoop中的RPC应用实例demo

    hadoop中的RPC框架实现机制 RPC是Remotr Process Call, 进程间的远程过程调用,不是在一个jvm里. 即,Controller拿不到Service的实例对象. hadoop ...

  9. 初识jQuery,八字真言“选择元素,对其操作”

    jQuery在我印象中,就是很多类似$(),然后昨天开始接触了,觉得很和谐,获取元素比JavaScript简单很多,有意思. 一.开始学习jQuery,下载jQuery库的文件 http://jque ...

  10. Coordinate System

    Coordinate System Introduction of Different Coordinate Systems Cartesian Coordinate System UI Coordi ...