目录
- AD验证
- 权限设计
一、AD(Active Directory)活动目录验证
1.概念
存储用户、计算机和网络资源的信息,并且使资源可以被用户和应用程序访问;它提供了命名、描述、定位、访问、管理和保护这些资源有关信息的一致路径。
2.作用
对网络资源的集中控制;在逻辑结构中安全地存储对象;优化网络流量。
3. AD的逻辑结构
组成节点:域、域树、林、林根域;
域:AD逻辑结构中的核心功能;
域提供的三种功能:对象的管理、边界管理共享资源安全性的方法、对象的复制单元;
林:AD完整实例,其中包括一个或多个树。
4. AD物理结构
节点:域控制器、AD站点
5.AD工作原理
目录服务:基于活动目录实现各种功能
6..NET对于AD的支持
System.DirectoryServices:以托管代码访问AD,包含两个组件类:
- DirectoryEntry类封装AD层次结构中的节点和对象,可以读取和更新对象和节点中的特性。
- DirectorySearcher类对AD层次结构执行查询。LDAP 是系统提供的唯一一种支持搜索的 Active Directory 服务接口 (ADSI) 提供程序,返回 SearchResult 的实例,这些实例包含在 SearchResultCollection 类的实例中。
7.AD验证系统设计
8. 实例代码
View Code
1 public partial class Form1 : Form
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7 private void Form1_Load(object sender, EventArgs e)
8 {
9 Try{
10 //操作IIS时,DirectoryEntry的Path的格式为:
11 IIS://ComputerName/Service/Website/Directory
12 string path = "LDAP://dc/OU=**,OU=**,DC=**,DC=**";
13 /* * CN:是对象在所在容器内的通用名称
14 * OU:是包含对象的组织单位。如果对象位于嵌套的组织单位中,则可能具有多个OU值
15 * DC:代表域部分,通常有两个域部分
16 * */
17 Enumerate(path);
18 treeView1.ExpandAll();
19 }
20 catch (Exception ex)
21 {
22 throw ex;
23 }
24 }
25
26 protected void Enumerate(string path)
27 {
28
29 using (DirectoryEntry root = new DirectoryEntry(path))
30 {
31 TreeNode node = new TreeNode(root.Name);
32 treeView1.Nodes.Add(node);
33 EnumChildren(root, node);
34 }
35 }
36 protected void EnumChildren(DirectoryEntry entry,TreeNode treeNode)
37 {
38 if (entry.Children != null)
39 {
40 foreach (DirectoryEntry i in entry.Children)
41 {
42 EnumChildren(i, AddNode(treeNode, i.Name));
43 }
44 }
45 }
46 protected TreeNode AddNode(TreeNode parent, string text)
47 {
48 TreeNode child = new TreeNode(text);
49 parent.Nodes.Add(child);
50 return child;
51 }
52 }
二、权限设计
1. 常用的权限设计模式
模式一:用户-》角色-》权限
2. 数据库结构
2.1表结构
2.2 说明
Account:账户表
Role:角色表
AccountRole:账户角色关联表
Pentence:权限表
FunctionResource:资源操作表
2.3实例
测试数据:
View Code
1 CREATE TABLE dbo.Account
2 (
3 id char(36) primary key,
4 employee_id char(36),
5 login_name nvarchar(50),
6 password nvarchar(50),
7 isuse int,
8 online int
9 );
10
11 INSERT INTO dbo.Account
12 SELECT 1001,2001,'admin',123,1,1
13 UNION ALL
14 SELECT 1002,2002,'login_1',123,1,0
15
16 CREATE TABLE dbo.AccountRole
17 (
18 id char(36) primary key,
19 role_id char(36) references Role(id),
20 account_id char(36) references Account(id)
21 );
22
23 INSERT INTO dbo.AccountRole
24 SELECT 3001,4001,1001
25 UNION ALL
26 SELECT 3002,4002,1002
27
28 CREATE TABLE dbo.Role
29 (
30 id char(36) primary key,
31 role_name nvarchar(50),
32 ismax int,
33 descr nvarchar(100)
34
35 );
36 INSERT INTO dbo.Role
37 SELECT 4001,'a001',1,'abc1'
38 UNION ALL
39 SELECT 4002,'a002',0,'abc2'
40
41 CREATE TABLE dbo.Pentence
42 (
43 id char(36) primary key,
44 role_id char(36) references Role(id),
45 function_code int references FunctionResource(function_code)
46 );
47
48 INSERT INTO dbo.Pentence
49 SELECT 5001,4001,001001
50 UNION ALL
51 SELECT 5002,4002,001002
52
53 CREATE TABLE dbo.Pentence
54 (
55 id char(36) primary key,
56 role_id char(36) references Role(id),
57 function_code int references FunctionResource(function_code)
58 );
59
60 INSERT INTO dbo.Pentence
61 SELECT 5001,4001,001001
62 UNION ALL
63 SELECT 5002,4002,001001001
64
65 CREATE TABLE dbo.FunctionResource
66 (
67 function_code int primary key,
68 name nvarchar(50),
69 url nvarchar(50),
70 type int,
71 parent_code int
72 );
73
74 INSERT INTO dbo.FunctionResource
75 SELECT 001001,'合同管理','',0,''
76 UNION ALL
77 SELECT 001001001,'设备管理','',0,001001
78 UNION ALL
79 SELECT 001001001001,'基本状态','',1,001001001
示意性代码:
View Code
1 select * from Account where login_name='admin'
2 select * from AccountRole where account_id='1001'
3 select * from Role where id='4001'
4 select * from Pentence where role_id='4001';
5
6 with rights as(select fr.*,1 as level from FunctionResource fr where parent_code='0'
7 union all
8 select fr1.*,(r.level+1) as level from FunctionResource fr1
9 inner join rights r on fr1.parent_code=r.function_code)
10
11 select r.*,
12 case level
13 when 1 then ''+name
14 when 2 then '|- '+name
15 when 3 then '|- |- '+name
16 when 4 then '|- |- |- '+name
17 end as tree from rights r
18 order by function_code
2.4效果图
3. 在控制层写if/else判断条件
用户登录系统后,就根据登录的账号信息找到该用户的角色,将该角色的所有的菜单权限和操作按钮权限加载到列表中,保存到Session,一直到用户退出系统或者session过期。