如果你用的是ASP.NET那么我建议你去看看context Identity Principal 之类的ASP.NET内建的身份验证体制。ASP.NET 一共有windows passport form 这样几种认证。你还可以看看“身份验证票“之类的技术,这样你在结合数据库就很方便的实现你说的代码。 如果你用的是ASP 你就用以上各位朋友说的session来实现。
--参考: /*--软件权限设置控制的例子 --*/CREATE TABLE 功能 ( id int IDENTITY (1, 1), 功能名称 nvarchar (50), parentid int ) GOCREATE TABLE 用户 ( id int IDENTITY (1, 1) NOT NULL, 用户名 nvarchar (50), 密码 nvarchar (50), 修改日期 datetime, 起始日期 datetime ) GOCREATE TABLE 权限 ( id int IDENTITY (1, 1) NOT NULL, 用户ID int, 功能id int, 操作权限 int, ) GO--数据 --软件的功能分类 insert into 功能 select '产品销售',0 union all select '电器',1 union all select '食品类',1 union all select '日常用品',1 union all select '家电',2 union all select '电脑',2--用户 insert into 用户 select '张三',null,getdate(),getdate() union all select '李四',null,getdate(),getdate()--权限分配,假设每个功能都有这样的权限设置:1.新增/2.修改/3.删除/4.打印 insert into 权限 select 1,2,power(2,1)+power(2,2)+power(2,4) --张三具有电器类的1.新增/2.修改/4.打印的功能 union all select 1,4,power(2,1)+power(2,2) --张三具有日常用品类的1.新增/2.修改的功能 union all select 2,1,power(2,1)+power(2,2)+power(2,3)+power(2,4) --李四具有所有权限 /* select * from 权限 drop table 权限 比如说,我想查询张三对电器类有什么权限的时候我应该怎么写? 谁能仔细说一下这中权限设计的原理??? */--将邹老大的题改一下,变成查李四的,李四有所有权限,按说也应有电器类的权限 --但结果不符 select 新增=case power(2,1) & 操作权限 when power(2,1) then '√' else '' end ,修改=case power(2,2) & 操作权限 when power(2,2) then '√' else '' end ,删除=case power(2,3) & 操作权限 when power(2,3) then '√' else '' end ,打印=case power(2,4) & 操作权限 when power(2,4) then '√' else '' end from 权限 a join 用户 b on a.用户ID=b.id and b.用户名='李四' join 功能 c on a.功能ID=c.id and c.功能名称='电器' 52.--如何使用的示例:--测试数据 create table 角色表(RoleID int,RoleName varchar(10)) insert into 角色表 select 1,'Admin' union all select 2,'cwb' union all select 3,'manage'create table 权限表(RightID int,RightName varchar(50)) insert into 权限表 select 1,'M1_Browser' union all select 2,'M1_Add' union all select 3,'M1_Modify' union all select 4,'M1_Delete' union all select 5,'M1_Print' union all select 6,'M2_Browser' union all select 7,'M2_Print' union all select 8,'M3_Browser' union all select 9,'M3_Add' union all select 10,'M3_Modify' union all select 12,'M3_Print'create table 角色权限表(RoleID int,RightID int) insert into 角色权限表 select 1,1 union all select 1,2 union all select 1,3 union all select 1,6 union all select 2,6 union all select 2,7 go--创建显示的存储过程 create proc p_show @RoleName varchar(10) as select 模块=模块 ,browser=max(case 权限 when 'browser' then 1 else 0 end) ,[Add]=max(case 权限 when 'Add' then 1 else 0 end) ,[Modify]=max(case 权限 when 'Modify' then 1 else 0 end) ,[delete]=max(case 权限 when 'delete' then 1 else 0 end) ,[print]=max(case 权限 when 'print' then 1 else 0 end) from( select 模块=left(RightName,charindex('_',RightName)-1) ,权限=substring(RightName,charindex('_',RightName)+1,50) from 角色权限表 a join 权限表 b on a.RightID=b.RightID where RoleID=(select RoleID from 角色表 where RoleName=@RoleName) ) a group by 模块 go--创建保存的存储过程 create proc p_save @RoleName varchar(10), --要保存的角色名称 @tbname sysname, --用户选择处理后的结果表名 @deletetable bit=1 --处理完成后是否自动删除结果表,1.(默认)自动删除 0.不删除 as declare @RoleID int,@s nvarchar(4000) select @RoleID=RoleID from 角色表 where RoleName=@RoleName SET XACT_ABORT ON begin tran delete 角色权限表 where RoleID=@RoleID set @s='insert into 角色权限表(RoleID,RightID) select @RoleID,a.RightID from 权限表 a join( select RightName=模块+''_browser'' from ['+@tbname+'] where browser=1 union all select 模块+''_Add'' from ['+@tbname+'] where [Add]=1 union all select 模块+''_Modify'' from ['+@tbname+'] where [Modify]=1 union all select 模块+''_delete'' from ['+@tbname+'] where [delete]=1 union all select 模块+''_print'' from ['+@tbname+'] where [print]=1 ) b on a.RightName=b.RightName' exec sp_executesql @s,N'@RoleID int',@RoleID if @deletetable=1 exec('drop table ['+@tbname+']') commit tran go--创建临时表表,保存显示结果 create table #t(模块 varchar(10),browser bit,[Add] bit,[Modify] bit,[delete] bit,[print] bit) insert into #t exec p_show 'admin'--修改权限 update #t set browser=1,[Add]=1,[Modify]=1--进行权限保存 exec p_save 'admin','#t'--显示保存结果 exec p_show 'admin' go--删除测试环境 drop table 角色表,权限表,角色权限表 drop proc p_show,p_save
再建一個表,儲存用戶中,處理(增、刪、改、審核、作費等)模塊的權限
我在WIN FORM中是這樣實現的,
WEB FORM也應該可以。
{
try
{
DataTable dt=new SysJuris().GetMyJuris(uid,key);
if(dt.Rows.Count==1)
{
bool Juris=Convert.ToBoolean(dt.Rows[0][key]);
return Juris;
}
else
{
return false;
}
}
catch
{
return false;
}
}
在登录时,把用户对像传给一个Session值,这样当用户要操作某个模块时,就可以取出该用户对像,用上面提到的方法判断权限.
具体的实现,程序细节就要你自己去琢磨了.
如果你用的是ASP 你就用以上各位朋友说的session来实现。
/*--软件权限设置控制的例子
--*/CREATE TABLE 功能 (
id int IDENTITY (1, 1),
功能名称 nvarchar (50),
parentid int
)
GOCREATE TABLE 用户 (
id int IDENTITY (1, 1) NOT NULL,
用户名 nvarchar (50),
密码 nvarchar (50),
修改日期 datetime,
起始日期 datetime
)
GOCREATE TABLE 权限 (
id int IDENTITY (1, 1) NOT NULL,
用户ID int,
功能id int,
操作权限 int,
)
GO--数据
--软件的功能分类
insert into 功能
select '产品销售',0
union all select '电器',1
union all select '食品类',1
union all select '日常用品',1
union all select '家电',2
union all select '电脑',2--用户
insert into 用户
select '张三',null,getdate(),getdate()
union all select '李四',null,getdate(),getdate()--权限分配,假设每个功能都有这样的权限设置:1.新增/2.修改/3.删除/4.打印
insert into 权限
select 1,2,power(2,1)+power(2,2)+power(2,4) --张三具有电器类的1.新增/2.修改/4.打印的功能
union all select 1,4,power(2,1)+power(2,2) --张三具有日常用品类的1.新增/2.修改的功能
union all select 2,1,power(2,1)+power(2,2)+power(2,3)+power(2,4) --李四具有所有权限
/*
select * from 权限
drop table 权限
比如说,我想查询张三对电器类有什么权限的时候我应该怎么写?
谁能仔细说一下这中权限设计的原理???
*/--将邹老大的题改一下,变成查李四的,李四有所有权限,按说也应有电器类的权限
--但结果不符
select 新增=case power(2,1) & 操作权限 when power(2,1) then '√' else '' end
,修改=case power(2,2) & 操作权限 when power(2,2) then '√' else '' end
,删除=case power(2,3) & 操作权限 when power(2,3) then '√' else '' end
,打印=case power(2,4) & 操作权限 when power(2,4) then '√' else '' end
from 权限 a
join 用户 b on a.用户ID=b.id and b.用户名='李四'
join 功能 c on a.功能ID=c.id and c.功能名称='电器'
52.--如何使用的示例:--测试数据
create table 角色表(RoleID int,RoleName varchar(10))
insert into 角色表
select 1,'Admin'
union all select 2,'cwb'
union all select 3,'manage'create table 权限表(RightID int,RightName varchar(50))
insert into 权限表
select 1,'M1_Browser'
union all select 2,'M1_Add'
union all select 3,'M1_Modify'
union all select 4,'M1_Delete'
union all select 5,'M1_Print'
union all select 6,'M2_Browser'
union all select 7,'M2_Print'
union all select 8,'M3_Browser'
union all select 9,'M3_Add'
union all select 10,'M3_Modify'
union all select 12,'M3_Print'create table 角色权限表(RoleID int,RightID int)
insert into 角色权限表
select 1,1
union all select 1,2
union all select 1,3
union all select 1,6
union all select 2,6
union all select 2,7
go--创建显示的存储过程
create proc p_show
@RoleName varchar(10)
as
select 模块=模块
,browser=max(case 权限 when 'browser' then 1 else 0 end)
,[Add]=max(case 权限 when 'Add' then 1 else 0 end)
,[Modify]=max(case 权限 when 'Modify' then 1 else 0 end)
,[delete]=max(case 权限 when 'delete' then 1 else 0 end)
,[print]=max(case 权限 when 'print' then 1 else 0 end)
from(
select 模块=left(RightName,charindex('_',RightName)-1)
,权限=substring(RightName,charindex('_',RightName)+1,50)
from 角色权限表 a
join 权限表 b on a.RightID=b.RightID
where RoleID=(select RoleID from 角色表 where RoleName=@RoleName)
) a group by 模块
go--创建保存的存储过程
create proc p_save
@RoleName varchar(10), --要保存的角色名称
@tbname sysname, --用户选择处理后的结果表名
@deletetable bit=1 --处理完成后是否自动删除结果表,1.(默认)自动删除 0.不删除
as
declare @RoleID int,@s nvarchar(4000)
select @RoleID=RoleID from 角色表 where RoleName=@RoleName
SET XACT_ABORT ON
begin tran
delete 角色权限表 where RoleID=@RoleID
set @s='insert into 角色权限表(RoleID,RightID)
select @RoleID,a.RightID
from 权限表 a join(
select RightName=模块+''_browser'' from ['+@tbname+'] where browser=1
union all select 模块+''_Add'' from ['+@tbname+'] where [Add]=1
union all select 模块+''_Modify'' from ['+@tbname+'] where [Modify]=1
union all select 模块+''_delete'' from ['+@tbname+'] where [delete]=1
union all select 模块+''_print'' from ['+@tbname+'] where [print]=1
) b on a.RightName=b.RightName'
exec sp_executesql @s,N'@RoleID int',@RoleID
if @deletetable=1 exec('drop table ['+@tbname+']')
commit tran
go--创建临时表表,保存显示结果
create table #t(模块 varchar(10),browser bit,[Add] bit,[Modify] bit,[delete] bit,[print] bit)
insert into #t exec p_show 'admin'--修改权限
update #t set browser=1,[Add]=1,[Modify]=1--进行权限保存
exec p_save 'admin','#t'--显示保存结果
exec p_show 'admin'
go--删除测试环境
drop table 角色表,权限表,角色权限表
drop proc p_show,p_save
功能ID 功能名称 URL 权限ID
1 功能A /FuncA.aspx 1
2 功能A1 /FuncA1.aspx 2
3 功能A1 /FuncA1.aspx 3
27 功能C /FuncC.aspx 4
28 功能X /FuncX.aspx 5--用记表
用户ID
功能ID 功能名称 功能页面URL 对应权限ID
1 功能A /FuncA.aspx 1
2 功能A1 /FuncA1.aspx 2
3 功能A1 /FuncA1.aspx 3
27 功能C /FuncC.aspx 4
28 功能X /FuncX.aspx 5
……
--(2)用户表
用户ID/账号/姓名等 用户权限串(1有权/0无权)
1 admin 管理员 11111……
2 test 测试员 11001……
--(3)在每个需限制功能权限的页面中,加权限判断
如FuncC.aspx,——取当前用户权限串中的第“4”位值,
——如果为1,有权执行,继续;
——如果为0,提示用户无权执行此功能。
……
郁闷,
做程序的人咋就这么变态呐。
puke
puke
puke
puke
puke