近来这段时间blog园有几位同学在探讨通用的权限方案,偶闲来无事,也来凑凑热闹,下面基本说一下我的基本处理方案,基于AOP的。由于运用了Asp.Net MVC 开发,可能须要先对MVC有些明白,思路都是差不多的。
1.数据结构
![一个基于Asp.Net MVC的权限方案[组图]图片1](/Files/game/2010-2/28/1022813160227007.png)
Mad_Popedom为权限表,Control记载控制器名,Action记载动作名。
Mad_Role为角色表。
2.权限控制的实现
此处运用比较基本AOP形式,用MVC的Filter实现,代码如下
1 using System.Collections.Generic;
2 using System.Web.Mvc;
3 using Madnet.Model.MadAdmin;
4 using Madnet.BLL.MadAdmin;
5
6 namespace Madnet.Controllers.MadAdmin
7 {
8 public class SupportFilterAttribute : ActionFilterAttribute
9 {
10 private bool _IsLogin = true;
11 /// <summary>
12 /// 能无法须要登录
13 /// </summary>
14 public bool IsLogin
15 {
16 set
17 {
18 _IsLogin = value;
19 }
20 get
21 {
22 if (System.Configuration.ConfigurationManager.AppSettings["IsLogin"] != null)
23 {
24 bool.TryParse(System.Configuration.ConfigurationManager.AppSettings["IsLogin"].ToString(), out _IsLogin);
25 }
26 return _IsLogin;
27 }
28 }
29 public override void OnActionExecuting(ActionExecutingContext filterContext)
30 {
31 string controllerName = (string)filterContext.RouteData.Values["controller"];
32 string actionName = (string)filterContext.RouteData.Values["action"];
33
34 if (IsLogin && filterContext.HttpContext.Session["Login_User"] == null)
35 {
36 filterContext.HttpContext.Response.Redirect(new UrlHelper(filterContext.RequestContext).Action("Login", "Default"));
37 filterContext.Result = new EmptyResult();
38 }
39 else if (IsLogin && filterContext.HttpContext.Session["Login_User"] != null)
40 {
41 Mad_User user = filterContext.HttpContext.Session["Login_User"] as Mad_User;
42 if (!user.is_super)
43 {
44 if (!GetPopedom(user).Exists(p => p.Controller_Name == controllerName.ToLower() && p.Action_Name == actionName.ToLower()))
45 {
46 filterContext.HttpContext.Response.Write("没有权限");
47 filterContext.Result = new EmptyResult();
48 }
49
50 }
51 }
52
53 }
54 /// <summary>
55 /// 获取当前用户所有有权限执行的动作
56 /// </summary>
57 /// <returns></returns>
58 public List<Atmodel> GetPopedom(Mad_User user)
59 {
60 List<Atmodel> ats = new List<Atmodel>();
61 List<Mad_Popedom> pops = Mad_PopedomBLL.GetPopedombyUser(user.user_id);
62 foreach (Mad_Popedom pop in pops)
63 {
64 ats.Add(new AtModel() { Controller_Name = pop.Control, Action_Name = pop.Action });
65 }
66 return ats;
67 }
68
69 }
70 }
解释一下,上面的代码就是在执行前,先获取登录用户能够运行的Controller-Action,然后和当前须要执行的Controller-Action比较,如存在,即议决,否则为没有权限执行。
3.为动作添加权限
为基本起见,对于Controller层我是独立出来一个类库的,优点是等会为角色添加权限的时刻咱们不须要手动输入,只要反射dll就能够了。
![一个基于Asp.Net MVC的权限方案[组图]图片2](/Files/game/2010-2/28/1022813160247479.png)
如图所示,凡须要权限控制的函数,只须要添加[SupportFilter]特征就能够了,当然这种形式只好控制到Action级。
4.为角色额添加权限
这个比较基本,只须要把角色和权限关联起来就能够了,这里我是用反射Controller层dll实现。
Web.config
![一个基于Asp.Net MVC的权限方案[组图]图片3](/Files/game/2010-2/28/1022813160229979.png)
Global.asax.cs
![一个基于Asp.Net MVC的权限方案[组图]图片4](/Files/game/2010-2/28/1022813160360648.png)
Madnet.Controllers.Test即为Controller层的dll
![一个基于Asp.Net MVC的权限方案[组图]图片5](/Files/game/2010-2/28/1022813160316363.png)
Test为Controller名,index为Action名,挑选role2能够访问的Action,提交到数据库即可。此图示意role2拥有Test1Controller的访问权限,但是没有Test2Controller,Test3Controller的访问权限。
5.结束
上面4步即已完成基本的权限控制。能够在此基本上加上用户组,用户,菜单等维护,可实现”用户-角色-权限”的自由组合,一个基本的通用后台大概就是这样了。
点击查看大图
读库教程网文章由网络收集后整理发布,文章发布人拥有该内容的所有权力及责任!
如果你喜欢这页,可以按Ctrl+D收藏起来。







