ASP.NET MVC开发专题博客

ASP.NET MVC开发专题博客,为您精选ASP.NET MVC开发教程,助您开发愉快!

公告信息
欢迎光临ASP.NET MVC开发专题博客,祝您开发愉快!
文章档案
最新评论

ASP.NET MVC权限设计实践:Action参数验证权限

 本节介绍:ASP.NET MVC权限设计实践:Action参数验证权限。

 在本节之前,我们希望用ASP.NET MVC设置出一个相对合成熟的权限系统。

 不过总有一些问题,困扰着我们,如:

 1.太过依赖ASP.NET MVC架构的权限设计,不通用。

 2.权限控制过于复杂。

 3.感觉比较莫名的纠心!

 

我们先上一个模型:Role角色类。

 

然后我们进行账号管理的代码控制:AccountController。

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using Domain;
7 using Repository;
8 using Moq;
9 using WebUI.Views.Account;
10
11 namespace WebUI.Controllers
12 {
13 public class AccountController : Controller
14 {
15 private IUserRepository<SysUser> _Repository;
16
17 //
18 // GET: /Account/
19 [HttpGet]
20 public ActionResult Login()
21 {
22 return View();
23 }
24
25 [HttpPost]
26 public ActionResult Login(Account_LoginVM FormUser)
27 {
28 //初始化模拟数据
29 this.initMockData();
30
31 SysUser DbUser = this._Repository.findByUserID(FormUser.UserID);
32
33 if (DbUser == null)
34 {
35 HttpContext.Response.Write("不存在此用户!");
36 HttpContext.Response.End();
37 }
38 else if (FormUser.UserPwd != DbUser.UserPwd)
39 {
40 HttpContext.Response.Write("密码错误!");
41 HttpContext.Response.End();
42 }
43
44 HttpContext.Session["User"] = DbUser;
45
46 return RedirectToRoute(new { controller = "Home", action = "Index" });
47 }
48
49 private void initMockData()
50 {
51 var mock =new Mock<IUserRepository<SysUser>>();
52
53 //角色链
54 var roleList= new List<UserRole>()
55 {
56 new UserRole()
57 {
58 Id=1,
59 RoleName="Register",
60 },
61 //暂时去掉Admin权限。
62 // new UserRole()
63 //{
64 // Id=2,
65 // RoleName="Admin",
66 //}
67 };
68
69 mock.Setup(m => m.findByUserID("HuntSoul"))
70 .Returns
71 (
72 new SysUser()
73 {
74 Id = 1,
75 UserID = "HuntSoul",
76 UserPwd = "654321",
77 NickName = "BackDoor", //UI Modual,用于ACTION参数验证用的。
78 Roles = roleList
79 }
80 );
81
82 _Repository = mock.Object;
83 }
84 }
85 }
86

我们输入用户名和密码:

结果提示没有权限,这是因为过滤器对角色和用户账号进行了范围限制。


1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using WebUI.ActionFilter;
7
8 namespace WebUI.Controllers
9 {
10 [HandleError]
11 public class HomeController : Controller
12 {
13 //用户通用过滤器,可以配置角色和用户ID的权限组合来过滤。
14 [UserAuthorize
15 (
16 Roles =new string[]{ "Admin"},
17 UserID=new string[]{"Admin"}
18 )
19 ]
20 public ActionResult Index()
21 {
22 ViewData["Message"] = "Index Action 验证成功!";
23
24 return View();
25 }
26
27 //Action级别下参数过滤
28 [FilterByUserInfo
29 (
30 Roles=new string[]{"Admin"}
31 )
32 ]
33 public ActionResult Manage(string Modual)
34 {
35 ViewData["Message"] = "模块进入验证成功!";
36
37 return View();
38 }
39 }
40 }
41

再往下看示例代码:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using Domain;
7
8 namespace WebUI.ActionFilter
9 {
10 public class UserAuthorizeAttribute:ActionFilterAttribute
11 {
12 /// <summary>
13 /// 角色链
14 /// </summary>
15 public string[] Roles { get; set; }
16
17 /// <summary>
18 /// 用户ID链
19 /// </summary>
20 public string[] UserID { get; set; }
21
22 protected SysUser _User { get; set; }
23
24 public override void OnActionExecuting(ActionExecutingContext filterContext)
25 {
26 _User = HttpContext.Current.Session["User"] as SysUser;
27
28 var rolesResult = from a in _User.Roles
29 from b in Roles
30 where a.RoleName.Equals(b)
31 select a;
32
33 var userIDResult = from a in _User.UserID
34 from b in UserID
35 where a.Equals(b)
36 select a;
37
38 if (rolesResult.ToList().Count == 0 && userIDResult.ToList().Count == 0)
39 {
40 HttpContext.Current.Response.Write("没有权限!");
41 HttpContext.Current.Response.End();
42 }
43 }
44
45 public override void OnResultExecuted(ResultExecutedContext filterContext)
46 {
47 base.OnResultExecuted(filterContext);
48 }
49 }
50 }

 接着我们再登陆看看:[得去掉Admin角色的注释]

 

这次提示Index Action 验证成功!

下面再给出:针对特定的Action再写一个特定的过滤器去处理权限分配的示例代码:

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5
6 namespace WebUI.ActionFilter
7 {
8 public class FilterByUserInfoAttribute:UserAuthorizeAttribute
9 {
10 public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
11 {
12 base.OnActionExecuting(filterContext);
13
14 //配置你想额外做参数权限控制的地方,有点别扭。
15 var moduel = filterContext.RouteData.Values["id"].ToString();
16
17 if (_User.NickName != moduel)
18 {
19 HttpContext.Current.Response.Write("没有权限!");
20 HttpContext.Current.Response.End();
21 }
22 }
23
24 public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
25 {
26 base.OnActionExecuted(filterContext);
27 }
28 }
29 }

本次ASP.NET MVC权限设计实践:Action参数验证权限一文就介绍到这里了。

本次介绍有点简单,主要还是以代码为主,如需要更详细的信息,可以百度google


如果涉及数据库操作,推荐一款配套的ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"

2011/9/8 0:57:06 | ASP.NET MVC教程 | |

#1Anonymous[Register][121.8.124.*]2012/3/12 15:46:05
如果不同的contoller 有相同的action,您的代码时候就不ok了
  • 发表评论