asp.net MVC 2 自定义用户角色权限设计

此地http://www.cnblogs.com/xiaoqi/archive/2011/01/24/1942880.html的博文,加上数据库,用entity framework稍作修改分享之。

实体模型如下图:

 

 DBUserAuthorizeAttribute.cs如下

DBUserAuthorizeAttribute.cs
  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace MVCRole.Models
8 {
9 /// <summary>
10 /// 自定义AuthorizeAttribute
11 /// </summary>
12 public class DBUserAuthorizeAttribute : AuthorizeAttribute
13 {
14 private UserInfoEntities Database = new UserInfoEntities();
15 public override void OnAuthorization( AuthorizationContext filterContext ) {
16 if (filterContext == null) {
17 throw new ArgumentNullException( "filterContext" );
18 }
19 User user = filterContext.HttpContext.Session["CurrentUser"] as User;
20 string controller = filterContext.RouteData.Values["controller"].ToString();
21 string action = filterContext.RouteData.Values["action"].ToString();
22 var isAllowed = this.IsAllowed( user, controller, action );
23 if (!isAllowed) {
24 filterContext.HttpContext.Response.StatusCode = 401;
25 }
26
27 }
28
29 /// <summary>
30 /// 判断是否允许访问
31 /// </summary>
32 /// <param name="user">用户</param>
33 /// <param name="controller">控制器</param>
34 /// <param name="action">action</param>
35 /// <returns>是否允许访问</returns>
36 public bool IsAllowed( User user, string controller, string action ) {
37
38 // 找controllerAction
39 var controllerAction = Database.ControllerActions.FirstOrDefault( ca => ca.IsController == false && ca.Name == action && ca.ControllerName == controller );
40
41 //action无记录,找controller
42 if (controllerAction == null) {
43 controllerAction = Database.ControllerActions.FirstOrDefault( ca => ca.IsController && ca.Name == controller );
44 }
45
46 // 无规则
47 if (controllerAction == null) {
48 return true;
49 }
50
51
52 // 允许没有角色的:也就是说允许所有人,包括没有登录的用户
53 if (controllerAction.IsAllowedNoneRoles) {
54 return true;
55 }
56 if (user==null) {
57 return false;
58 }
59 // 允许所有角色:只要有角色,就可以访问
60 if (controllerAction.IsAllowedAllRoles) {
61 int count = Database.UserRoles.Count( ur => ur.UserID == user.ID );
62 if (count > 0) {
63 return true;
64 }
65 else {
66 return false;
67 }
68 }
69
70 // 选出action对应的角色
71 var actionRoles = Database.ControllerActionRoles.ToList().FindAll( ca => ca.ControllerActionID == controllerAction.ID );
72
73 if (actionRoles.Count == 0) {
74 // 角色数量为0,也就是说没有定义访问规则,默认允许访问
75 return true;
76 }
77 var userHavedRolesids = Database.UserRoles.ToList().FindAll( ur => ur.UserID == user.ID ).Select( ca => ca.RoleID );
78 // 查找禁止的角色
79 var notAllowedRoles = actionRoles.FindAll( r => !r.IsAllowed ).Select( ca => ca.RoleID );
80 if (notAllowedRoles.Count() > 0) {
81 foreach (int roleId in notAllowedRoles) {
82 // 用户的角色在禁止访问列表中,不允许访问
83 if (userHavedRolesids.Contains( roleId )) {
84 return false;
85 }
86 }
87 }
88 // 查找允许访问的角色列表
89 var allowRoles = actionRoles.FindAll( r => r.IsAllowed ).Select( ca => ca.RoleID ).ToList();
90 if (allowRoles.Count > 0) {
91 foreach (int roleId in allowRoles) {
92 // 用户的角色在访问的角色列表
93 if (userHavedRolesids.Contains( roleId )) {
94 return true;
95 }
96 }
97 }
98 // 默认禁止访问
99 return false;
100 }
101
102 }
103 }

 

  1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6
7 namespace MVCRole.Models
8 {
9 /// <summary>
10 /// 自定义AuthorizeAttribute
11 /// </summary>
12 public class DBUserAuthorizeAttribute : AuthorizeAttribute
13 {
14 private UserInfoEntities Database = new UserInfoEntities();
15 public override void OnAuthorization( AuthorizationContext filterContext ) {
16 var user = filterContext.HttpContext.Session["CurrentUser"] as User;
17
18 var controller = filterContext.RouteData.Values["controller"].ToString();
19 var action = filterContext.RouteData.Values["action"].ToString();
20 var isAllowed = this.IsAllowed( user, controller, action );
21
22 if (!isAllowed) {
23 filterContext.RequestContext.HttpContext.Response.Write( "无权访问" );
24 filterContext.RequestContext.HttpContext.Response.End();
25 }
26
27 }
28
29 /// <summary>
30 /// 判断是否允许访问
31 /// </summary>
32 /// <param name="user">用户</param>
33 /// <param name="controller">控制器</param>
34 /// <param name="action">action</param>
35 /// <returns>是否允许访问</returns>
36 public bool IsAllowed( User user, string controller, string action ) {
37
38 // 找controllerAction
39 var controllerAction = Database.ControllerActions.FirstOrDefault( ca => ca.IsController == false && ca.Name == action && ca.ControllerName == controller );
40
41 //action无记录,找controller
42 if (controllerAction == null) {
43 controllerAction = Database.ControllerActions.FirstOrDefault( ca => ca.IsController && ca.Name == controller );
44 }
45
46 // 无规则
47 if (controllerAction == null) {
48 return true;
49 }
50
51
52 // 允许没有角色的:也就是说允许所有人,包括没有登录的用户
53 if (controllerAction.IsAllowedNoneRoles) {
54 return true;
55 }
56 if (user==null) {
57 return false;
58 }
59 // 允许所有角色:只要有角色,就可以访问
60 if (controllerAction.IsAllowedAllRoles) {
61 int count = Database.UserRoles.Count( ur => ur.UserID == user.ID );
62 if (count > 0) {
63 return true;
64 }
65 else {
66 return false;
67 }
68 }
69
70 // 选出action对应的角色
71 var actionRoles = Database.ControllerActionRoles.ToList().FindAll( ca => ca.ControllerActionID == controllerAction.ID );
72
73 if (actionRoles.Count == 0) {
74 // 角色数量为0,也就是说没有定义访问规则,默认允许访问
75 return true;
76 }
77 var userHavedRolesids = Database.UserRoles.ToList().FindAll( ur => ur.UserID == user.ID ).Select( ca => ca.RoleID );
78 // 查找禁止的角色
79 var notAllowedRoles = actionRoles.FindAll( r => !r.IsAllowed ).Select( ca => ca.RoleID );
80 if (notAllowedRoles.Count() > 0) {
81 foreach (int roleId in notAllowedRoles) {
82 // 用户的角色在禁止访问列表中,不允许访问
83 if (userHavedRolesids.Contains( roleId )) {
84 return false;
85 }
86 }
87 }
88 // 查找允许访问的角色列表
89 var allowRoles = actionRoles.FindAll( r => r.IsAllowed ).Select( ca => ca.RoleID ).ToList();
90 if (allowRoles.Count > 0) {
91 foreach (int roleId in allowRoles) {
92 // 用户的角色在访问的角色列表
93 if (userHavedRolesids.Contains( roleId )) {
94 return true;
95 }
96 }
97 }
98 // 默认禁止访问
99 return false;
100 }
101
102 }
103 }
HomeController.cs
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using MVCRole.Models;
7
8 namespace MVCRole.Controllers
9 {
10 [HandleError]
11 [DBUserAuthorize]
12 public class HomeController : Controller
13 {
14 public ActionResult Index()
15 {
16 ViewData["Message"] = "欢迎使用 ASP.NET MVC!";
17
18 return View();
19 }
20 public ActionResult Admin()
21 {
22 ViewData["Message"] = "只有管理员才能访问!";
23
24 return View("Index");
25 }
26 public ActionResult User()
27 {
28 ViewData["Message"] = "只要是注册用户就能访问!";
29
30 return View("Index");
31 }
32 public ActionResult UserOnly()
33 {
34 ViewData["Message"] = "只能是User才能能访问!";
35
36 return View("Index");
37 }
38
39 public ActionResult Login(string user)
40 {
41 Session["CurrentUser"] = new UserInfoEntities().Users.FirstOrDefault(u => u.UserName == user);
42 if (Session["CurrentUser"] != null)
43 {
44 ViewData["Message"] = "你已登录为" + user;
45 }
46
47 return View("Index");
48 }
49
50
51 public ActionResult About()
52 {
53 return View();
54 }
55 }
56 }

流程:



转载于:https://www.cnblogs.com/1971ruru/archive/2012/02/20/2359972.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
asp.net mvc 通用权限管理系统(响应布局)源码是基于asp.net(C#)MVC+前端bootstrap+ztree+lodash+jquery技术,采用bootstrap为前台开发展示UI,Web Api主要负责前端的逻辑交互,再结合jQuery Ajax+Web Api进行提交数据请求。 框架特色: 1、为了数据方便读写,语言的成熟性,选择asp.net开发效率更高效率更快; 2、系统架构采用:耦合性低、重用性高、部署快、可维护性高等优点的MVC框架进行搭建; 3、系统配置文件数据采用HttpRuntime.Cache进行缓存,使得程序在运行中效率更高、速度更快; 4、数据库的选型,根据系统的数据规模与需求方的相关要求,综合多方考量,调研多种数据库后选定mysql为系统数据库; 5、运行环境根据功能模块的特点,选型window server2008+IIS,部署更简洁; 6、使用报表导出开源组件NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作; 7、为了兼容更多的浏览器,让用户界面使用更友好。我们选择了响应式布局框架bootstrap; 8、系统报表我们采用了echarts开源软件,并且我们提供了非常炫酷的图形界面,特色是地图,另外还提供了柱状图、折线图、饼图、气泡图及四象限图等; 9、系统采用bootstrap响应式布局,这样面对不同分辨率设备灵活性强能够快捷解决多设备显示适应问题; 10、系统基于ASP.NET(C#) MVC +web api+Bootstrap +Jquery+ MYSQL前端采用响应式布局对页面兼容性显著提高; 系统优势: 代码可以满足大部分开发者的需求,让开发人员能省不少时间 ASP.NET MVC通用角色权限管理系统源码 更新日志: 2020-03-23 更新如下: 1、新增文件上传功能实例,保证文件能够无误上传保存并下载; 2、优化后台系统框架代码,删除以前大项目其它无用功能; 3、新增页面代码JS、css文件的压缩,使得系统运行速度更快; 4、系统登录页面新增验证校验过程,这样做到输入验证码时时验证; 5、优化系统加载页面提示数据加载功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值