基于功能(代码)的权限管理

      摘要:.NetFramework类库本身包含了基于角色的安全性的权限管理功能,.NET Framework 基于角色的安全性通过生成可供当前线程使用的主体信息来支持授权,而主体是用关联的标识构造的。标识(及其帮助定义的主体)可以基于 Windows 帐户,也可以是同 Windows 帐户无关的自定义标识。.NET Framework 应用程序可以根据主体的标识或角色成员条件(或者这两者)做出授权决定。角色是指在安全性方面具有相同特权的一组命名主体(如出纳或经理)。一个主体可以是一个或多个角色的成员。因此,应用程序可以使用角色成员条件来确定主体是否有权执行某项请求的操作。 基于角色的安全性的权限管理的特性,我们在项目权限设计的时候,必须首先确定该系统有哪些角色及用户,项目完成后就不能任意修改用户与角色。因此个人觉得不太适合很多Web应用。本文所讨论的是如何实现基于功能的权限管理,也可以说是基于代码的权限管理。(可能理解有误)

      该基于功能的权限管理目前没有经过太多的测试,也没有在任何项目中应用,如果您将在您的项目中应用,本人会非常乐意,但是出现Bug您可以自己修改源代码,本人不能保证提供支持.

主要内容

      1.概述

      2.如何定义组件权限

      3 、如何分配权限

      4、权限组件的简单介绍

      5、代码下载

      6、其他说明

一.概述

      基于功能的权限管理也是基于代码的权限管理,因此基于功能的权限管理可以控制模块权限,类权限,函数权限,甚至字段权限.在项目应用中,我们可以控制任何需要权限的操作.如添加、删除、修改、追加、审阅等等。

二.如何定义组件权限
 
       我们先看一个简单的权限组件的代码

None.gif [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Function = " 100100 " )]
None.gif 
public   class  Custom
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public Custom()dot.gif{}
InBlock.gif
InBlock.gif  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Operation
="101001010")]
InBlock.gif  
public string GetValue(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return str;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Operation
="101001011")]
InBlock.gif  
public string Add(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return str;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Operation
="101001012")]
InBlock.gif  
public string Modify(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return str;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
InBlock.gif  
public string View(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return str;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif 
None.gif
None.gif

      这个组件定义了一个类,对于项目来说也可以说是一个功能。该类需要一个"100100"的功能权限,如何调用方没有"100100" 的功能权限,则调用方将得到一个安全异常。再看一下该类的函数,对项目来说也可以是一个操作。
 该类有多个方法,每个方法有不同的权限要求,对于View函数,它只需要有类的权限即可以被正常调用("100100").而对于其他三个函数还必须满足他们各自的操作权限.如果不能满足权限,也将抛出安全异常.

      必须具有Module="100",Function="100100",Operation="101001010|101001011|101001012"的权限,此处的"|"不是指或者,而是必须包含所有的权限,否则抛出异常
  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Module="100",Function="100100",Operation="101001010|101001011|101001012")]
  public string GetValue(string str)
  {
   return str;
  }
  
  必须具有Module="100",Operation="101001011"的权限,否则抛出异常
  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Module="100",Operation="101001011")]
  public string Add(string str)
  {
   return str;
  }
  
  必须具有Operation="101001012"的权限,否则抛出异常
  [DefaultPrincipalPermissionAttribute(SecurityAction.Demand,Operation="101001012")]
  public string Modify(string str)
  {
   return str;
  }

三.如何分配权限
 
       本文主要讲的用户授权管理,而对于用户验证不在本文的讨论中.所以对于验证的代码只有简单的返回. 

None.gif class  Class1
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  [STAThread]
InBlock.gif  
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
string user = "admin";
InBlock.gif   
string pass = "";
InBlock.gif   
InBlock.gif   
//用户验证
InBlock.gif
   if (!CheckUser(user,pass))
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.WriteLine(
"用户验证失败!");
InBlock.gif    
return
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   GenericIdentity MyIdentity 
= new GenericIdentity(user);
InBlock.gif   
InBlock.gif   
//用户授权
InBlock.gif
   DefaultPrincipalInfo info = GetUserPrincipalInfo(user);
InBlock.gif  
InBlock.gif   DefaultProgramPrincipal MyPrincipal 
= new DefaultProgramPrincipal( MyIdentity,info);
InBlock.gif   MyPrincipal.PermissionFail 
+= new PrincipalPermissionEventHandler(MyPrincipal_PermissionFail);
InBlock.gif
InBlock.gif   Thread.CurrentPrincipal 
= MyPrincipal;
InBlock.gif   CustomLib.Custom c 
= null;
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    c 
= new CustomLib.Custom();
InBlock.gif    Console.WriteLine(c.GetValue(
"SurenSuyu"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(System.Security.SecurityException se)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(se.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.WriteLine(c.Add(
"SurenSuyu"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(System.Security.SecurityException se)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(se.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.WriteLine(c.Modify(
"SurenSuyu"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(System.Security.SecurityException se)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(se.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    Console.WriteLine(c.View(
"SurenSuyu"));
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(System.Security.SecurityException se)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(se.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
private static void MyPrincipal_PermissionFail(object sender, PrincipalPermissionEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   System.Diagnostics.Debug.WriteLine(
string.Format("对用户({0})的授权失败!",e.Name));
InBlock.gif   
//e.ThrowException = false;
ExpandedSubBlockEnd.gif
  }

InBlock.gif  
InBlock.gif  
private static bool CheckUser(string user,string pass)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return true
ExpandedSubBlockEnd.gif  }

InBlock.gif  
private static DefaultPrincipalInfo GetUserPrincipalInfo(string user)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return new DefaultPrincipalInfo("100|102","101101","101001001|101001011|101001010",false);
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif }

None.gif
None.gif

       上面的代码只是简单的授予用户权限.
 
       到目前为止整个权限管理流程已经实现.我们只是设置了组件的权限要求,然后在调用方分配用户的权限就完成了整个权限验证.我们不需要编写任何的权限验证代码.权限的验证都在该权限组件里自动完成.

四.权限组件的简单介绍

       基于功能的权限管理与基于角色的安全性的权限管理事实上实现原理一致,如果大家对该组件实现原理不是很清楚,大家可以先看看Dotnet帮助文档中的基于代码访问安全性与基于角色的安全性章节.
 
五.代码下载
 
      组件代码  下载
 
       测试代码 下载

六.其他说明

      该组件必须加入全局缓存(GAC)中,否则会抛出找不到权限组件的异常  [2006-09-28]

       没有测试Nunit
 
       本文原创,不允许转载

转载于:https://www.cnblogs.com/SurenSuyu/archive/2006/09/26/515487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值