今天主要講一下對於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 }
public class HomeController : Controller
WebForm的權限控制方法
自定義類對Page進行繼承
對其內置方法OnLoad進行重寫
然后將需要做權限控制的Page進行對自定義類的繼承

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 7 namespace ZX.B2C.GoodBaby.UBack 8 { 9 public class BacePage:Page 10 { 11 public BacePage() 12 { 13 // 14 // TODO: 在此處添加構造函數邏輯 15 // 16 } 17 BLL.UserInfo userInfoBLL = new BLL.UserInfo(); 18 protected override void OnInit(EventArgs e) 19 { 20 base.OnInit(e); 21 } 22 protected override void OnLoad(EventArgs e)//重寫頁面的load 23 { 24 BasePage_Load();//處理權限的Session的方法 25 base.OnLoad(e); 26 } 27 public void BasePage_Load() 28 { 29 30 if (Request.Cookies["AdminCookie"] == null)//在這里我們行判斷用戶有沒有登陸如果登陸了我們再判斷權限 31 { 32 Response.Write("<html><head><title>系統安全提示</title><script>alert('為了系統安全,請重新登陸');location.href='/Login/Index.aspx';</script></head><body></body></html>"); 33 Response.End(); 34 } 35 string urlpath = Request.Url.AbsolutePath;//取當前訪問的頁面 36 bool t =QuanXian.CheckPage(urlpath);//判斷當前頁面當前用戶是不是有權限 37 if (!t) 38 { 39 Response.Write("<html><head><title>系統安全提示</title><script>alert('您沒有權限進行當前操作,請重新選擇用戶登陸操作');location.href='/Home/Vi_Index.aspx'</script></head><body></body></html>"); 40 Response.End(); 41 } 42 43 } 44 } 45 }
public partial class Add : BacePage