在实际的开发当中,我们经常会涉及到权限访问等问题。在很多开发当中有些程序员喜欢用Request.ServerVariables[“Script_Name”]来得到路径,然后再根据Session中的值来判断有没有访问权限,这种方法不好的地方在于页面的路径不能随意的改变,不然造成的工作量非常的大。
本方法主要是基于
RBAC
(基于角色的访问控制),现写出主要的步骤
1.
创建表(模块表、角色表、角色权限设置表、用户表)
角色表:
TbRole
字段
类型
大小
约束
描述
ModuleID
SMALLINT
主键,不为空
模块编号
ModuleName
VARCHAR
30
唯一,不为空
模块中文名称
ModuleEName
VARCHAR
30
唯一,不为空
模块英文名字
角色表:
TbRole
字段
类型
大小
约束
描述
RoleID
SMALLINT
主键,不为空
角色编号
RoleName
VARCHAR
20
唯一,不为空
角色名称
RoleDesc
VARCHAR
100
角色描述
角色权限设置表:
TbRoleSet
字段
类型
大小
约束
描述
RoleSetID
SMALLINT
主键,不为空
角色设置编号
RoleID
SMALLINT
引用
TbRole
表
角色编号
ModuleID
SMALLINT
引用
TbModule
表
模块编号
用户表:
TbUser
字段
类型
大小
约束
描述
UserName
VARCHAR
20
主键,不为空
用户名
UserPassword
VARCHAR
10
不为空
密码
UserRole SMALLINT 引用TbRole表 角色
模块表是把系统的各个模块写在其中,然后在角色表中加入角色,再通过角色权限设置表把各个角色能访问的权限联系起来。
模块表:
角色表:
角色权限设置表:
从以上我们可以看出,总共有4个模块,两个角色,他们分别具有的权限为:
管理员(浏览、添加、修改、删除) 普通用户(浏览)
添加了角色以后,我们在添加用户时就可以设定角色了
参考图如下:
用户添加:
2. 用户控件
数据库设置好了以后,接下来要想在什么地方进行身份检查
因为整个网站的头应该是相同的,所以我们可以把验证信息放到一个用户控件中,以后只要把用户控件放到相应的网页即可
但是每个网页都是不同的功能模块,我们怎么能保证用户控件能够动态的识别呢?
接下来在用户控件中加入一个模块英文名字的属性
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)
//
模块英文名称
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)
private
string
moduleEName
=
“”
;
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**/
///
<summary>
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
///
模块英文名称属性
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
///
</summary>
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)
public
string
ModuleEName
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
set
![](http://cj.cnblogs.com/Images/dot.gif)
{moduleEName
=
value;}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
get
![](http://cj.cnblogs.com/Images/dot.gif)
{
return
moduleEName;}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
这样的话只要在相应的网页中设置一下用户控件即可实现(注:ModuleEName要和数据库中的模块英文名相同),不设置即代表此页面不需要验证
如:
浏览模块页面:
<
uc1:top
id
=”Top1″
runat
=”server”
ModuleEName
=”ViewBook”></
uc1:top
>
添加模块页面:
<
uc1:top
id
=”Top1″
runat
=”server”
ModuleEName
=”AddBook”></
uc1:top
>
修改模块页面:
<
uc1:top
id
=”Top1″
runat
=”server”
ModuleEName
=”EditBook”></
uc1:top
>
删除模块页面:
<
uc1:top
id
=”Top1″
runat
=”server”
ModuleEName
=”DeleteBook”></
uc1:top
>
3. 身份验证
在用户控件中,只读取到英文模块名是无法判断权限的,所以在登录中,一定要把登录的角色用Session保存下来(如:Session[“UserRole”])
那么,既然可以得到正在登录的用户的角色,也可以得到此模块的名称,那么当然可以到数据库中看是否有权限了
参考代码如下:
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)
private
void
Page_Load(
object
sender, System.EventArgs e)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(
!
IsPostBack)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
模块名称等于空,代表不需要进行验证
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
只有在模块名称不为空时,才进行权限验证
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(moduleEName
!=
“”
)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
Session为空,禁止登录,防止非登录用户
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(Session[
”
UserRole
”
]
==
null
||
Session[
”
UserRole
”
].ToString()
==
“”
)
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Response.Redirect(
”
提示页面
”
);
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
连接数据库
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
……
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
根据模块英文名称和角色ID进行查选
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlCommand sqlCmd
=
new
SqlCommand(
”
SELECT RoleSetID FROM TbModule a,TbRoleSet b WHERE
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
a.ModuleID
=
b.ModuleID AND ModuleEName
=
‘
” + moduleEName + ”
‘
AND RoleID
=
”
+ Session[
”
UserRole
”
].ToString() +
“”
,sqlCon);
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlCon.Open();
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
sqlDr
=
sqlCmd.ExecuteReader();
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
返回有记录说明有权限,否则没有权限访问该模块
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(
!
sqlDr.HasRows)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Response.Redirect(
”
提示页面
”
);
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
catch
![](http://cj.cnblogs.com/Images/dot.gif)
{}
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
finally
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](http://cj.cnblogs.com/Images/dot.gif)
{
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
释放
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
……
![](http://cj.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://cj.cnblogs.com/Images/OutliningIndicators/None.gif)