ASP.NET MVC 和 WebForm的权限控制

今天主要讲一下对于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 }
View Code
 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 }
View Code
 public partial class Add : BacePage
原文地址:https://www.cnblogs.com/shanshanlaichi/p/6635250.html