今天主要讲一下对于ASP.NET的页面级权限控制
数据结构:用户表、角色表、权限表、角色权限派生表
为用户添加权限的数据配置后,
自定义类对MVC继承Controller
对其内置方法Initialize进行重写。
对其进行登录判断和权限判断
然后将需要做权限控制的Controller进行对自定义类的继承
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace ZX.B2C.GoodBaby.UI.App_Start 8 { 9 public class BaseClass : Controller 10 { 11 protected override void Initialize(System.Web.Routing.RequestContext requestContext) 12 { 13 base.Initialize(requestContext); 14 if (!IsLogin()) 15 { 16 Response.Redirect("/Home/toLogin"); 17 } 18 string urlpath = Request.Url.AbsolutePath; 19 bool t = CheckPage(urlpath);//判断当前页面当前用户是不是有权限 20 if (!t) 21 { 22 Response.Write("<html><head><title>系统安全提示</title><script>alert('您没有权限进行当前操作,请重新选择用户登陆操作');location.href='/Home/Vi_Index.aspx'</script></head><body></body></html>"); 23 Response.End(); 24 } 25 } 26 protected Boolean IsLogin() 27 { 28 if (Request.Cookies["GoodBabyMemberCookie"] != null) 29 { 30 return true; 31 } 32 else 33 { 34 return false; 35 } 36 } 37 static bool CheckPage(string urlpath) 38 { 39 if (urlpath == "/Home/Index" || urlpath == "/Home/Vi_Index") 40 { 41 return true; 42 } 43 else 44 { 45 #region 自身业务逻辑对权限控制的判断 46 47 BLL.SystemInfo systemInfoBLL = new BLL.SystemInfo(); 48 ZX.B2C.GoodBaby.Model.UserInfo userInfoModel = new Model.UserInfo(); 49 50 userInfoModel.UserInfoId = Convert.ToInt32(ZX.B2C.GoodBaby.Common.CookieHelper.GetCookieValue("GoodBabyMemberCookie")); 51 List<Model.SystemInfo> systemInfoList = systemInfoBLL.UserRoleSystemInfoList(userInfoModel.UserInfoId); 52 systemInfoList = systemInfoList.Where(p => p.SystemInfoUrl == urlpath).ToList(); 53 if (systemInfoList != null) 54 { 55 if (systemInfoList.Count > 0) 56 { 57 return true; 58 } 59 else 60 { 61 return false; 62 } 63 } 64 else 65 { 66 return false; 67 } 68 69 #endregion 70 71 } 72 } 73 } 74 }