Enterprise Library 2.0 -- Security Application Block

时间:2021-02-02 18:23:14

       今天来介绍一下Enterprise Library 2.0中的Security Application Block ,它可以帮助程序开发者在应用程序中实现与授权有关的大部分功能,不仅如此,它还支持缓存我们系统中用户的授权以及,授权数据。主要包括一下几个部分的功能:
1、减少代码编写量;
2、保持应用程序中安全管理的一致性;
3、提供了应用程序中很多安全问题的实现方法;
4、可扩展,支持自定义Provider。
...

       关于Security Application Block的介绍部分,我不再多说,具体可以参考帮助文档。下面我们来看一下如何配置Security Application Block,首先打开配置工具,新建一个Security Application Block,如下:
Enterprise Library 2.0 -- Security Application Block

然后在Authorization节点下创建一个Authorization Rule Provider,名称为RuleProvider,如下:

Enterprise Library 2.0 -- Security Application Block

现在就可以在RuleProvider节点下,创建一系列的规则(Rule)了。如下图:

Enterprise Library 2.0 -- Security Application Block

新建了规则,我们还需要为这个规则定义一个表达式,用于在程序中判断是否满足该规则,如下图:

Enterprise Library 2.0 -- Security Application Block

如图所示,我们定义了能通过该规则的用户的身份必须是User或Admin.我们可以根据我们的实际需要定义各种各样的规则。
最后我们可以为我们建立的Security Application Block分配一个默认的Authorization实例,如图:
Enterprise Library 2.0 -- Security Application Block

此时,我们的配置文件中就多了如下内容:

Enterprise Library 2.0 -- Security Application Block< section  name ="securityConfiguration"  type ="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"   />
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block
< securityConfiguration  defaultAuthorizationInstance ="RuleProvider"
Enterprise Library 2.0 -- Security Application Block    defaultSecurityCacheInstance
="" >
Enterprise Library 2.0 -- Security Application Block    
< authorizationProviders >
Enterprise Library 2.0 -- Security Application Block      
< add  type ="Microsoft.Practices.EnterpriseLibrary.Security.AuthorizationRuleProvider, Microsoft.Practices.EnterpriseLibrary.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 -- Security Application Block        name
="RuleProvider" >
Enterprise Library 2.0 -- Security Application Block        
< rules >
Enterprise Library 2.0 -- Security Application Block          
< add  expression ="R:User OR R:Admin"  name ="Rule"   />
Enterprise Library 2.0 -- Security Application Block        
</ rules >
Enterprise Library 2.0 -- Security Application Block      
</ add >
Enterprise Library 2.0 -- Security Application Block    
</ authorizationProviders >
Enterprise Library 2.0 -- Security Application Block  
</ securityConfiguration >

       关于Security Application Block的配置就说到这里。

      下面来看看Enterprise Library 2.0 的Security Application Block 与1.0版本中的一点区别,因为ASP.NET2.0中提供了角色与成员管理的新特性,所以在某些方面和1.0版本还是有一定的区别:
    因为ASP.NET2.0提供了MemberShip,Roles两个类,其中包含了验证用户、角色管理以及对Profile信息的设置与读取功能,所以Security Application Block 2.0将这些功能去掉了。因此和1.0版本的主要区别在于下面三方面:

首先导入下面两个命名空间:
using System.Web.Profile;
using System.Web.Security;

1、验证用户的方法如下:

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 验证用户
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="username">用户名</param>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="password">密码</param>
Enterprise Library 2.0 -- Security Application Block        
/// <returns></returns>

Enterprise Library 2.0 -- Security Application Block         public   bool  CheckUser( string  username,  string  password)
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            
return Membership.ValidateUser(username, password);
Enterprise Library 2.0 -- Security Application Block        }

2、读取或设置Profile信息

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 使用 Profile
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>

Enterprise Library 2.0 -- Security Application Block        [TestMethod]
Enterprise Library 2.0 -- Security Application Block        
public   void  UseProfile()
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            IIdentity identity;
Enterprise Library 2.0 -- Security Application Block            identity 
= new GenericIdentity("SHY520", Membership.Provider.Name);
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//设置Profile
Enterprise Library 2.0 -- Security Application Block
            ProfileBase setuserProfile = ProfileBase.Create(identity.Name);
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            setuserProfile[
"Name"= "SHY520";
Enterprise Library 2.0 -- Security Application Block            setuserProfile[
"Address"= "Wuhu China";
Enterprise Library 2.0 -- Security Application Block            
//保存Profile信息
Enterprise Library 2.0 -- Security Application Block
            setuserProfile.Save();
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//读取Profile信息
Enterprise Library 2.0 -- Security Application Block
            ProfileBase readuserProfile = ProfileBase.Create(identity.Name);
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
string Name = readuserProfile["Name"].ToString();
Enterprise Library 2.0 -- Security Application Block            
string Address = readuserProfile["Address"].ToString();
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            Assert.AreEqual(Name,
"SHY520");
Enterprise Library 2.0 -- Security Application Block            Assert.AreEqual(Address,
"Wuhu China");
Enterprise Library 2.0 -- Security Application Block        }

3、得到某个用户所属角色列表

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 得到某用户所属角色列表
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>
Enterprise Library 2.0 -- Security Application Block        
/// <returns>角色列表</returns>

Enterprise Library 2.0 -- Security Application Block         public   string [] GetUserRoles()
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            IIdentity identity 
= new GenericIdentity("SHY520", Membership.Provider.Name);
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
return Roles.GetRolesForUser(identity.Name);
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block        }

      与上一个版本的区别基本上就是以上几点,下面我们来看看Security Application Block2.0的基本使用方法:

1、为已授权的用户分配一个Token(记号):
       Security Application Block将授权用户的身份信息放进缓存,并返回一个Token,所以我们在使用此方之前需要先配置一个Security Cache,具体方法我就不多说了,可以参考官方教程,下面贴出配置后的App.Config中的代码:

Enterprise Library 2.0 -- Security Application Block< securityCacheProviders >
Enterprise Library 2.0 -- Security Application Block      
< add  cacheManagerInstanceName ="Cache Manager"  defaultSlidingSessionExpirationInMinutes ="10"
Enterprise Library 2.0 -- Security Application Block        defaultAbsoluteSessionExpirationInMinutes
="60"  type ="Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.CachingStoreProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
Enterprise Library 2.0 -- Security Application Block        name
="Caching Store Provider"   />
Enterprise Library 2.0 -- Security Application Block
</ securityCacheProviders >

这段代码是在securityConfiguration节中的,其中cacheManagerInstanceName是我们在Caching Application Block中配置的实例。

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 使用SaveIdentity方法,保存用户身份,并返回一个Token
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>
Enterprise Library 2.0 -- Security Application Block        
/// <returns></returns>

Enterprise Library 2.0 -- Security Application Block         public  IToken SaveUserInfo()
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            ISecurityCacheProvider sc 
= SecurityCacheFactory.GetSecurityCacheProvider("Cache Manager");
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//将用户身份装入缓存,并返回一个IToken类型的记号
Enterprise Library 2.0 -- Security Application Block
            return sc.SaveIdentity(new GenericIdentity("SHY520"));
Enterprise Library 2.0 -- Security Application Block        }

2、使用Token来验证用户是否已被授权

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 根据Token来判断用户是否已被授权
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="token"></param>
Enterprise Library 2.0 -- Security Application Block        
/// <returns></returns>

Enterprise Library 2.0 -- Security Application Block         public   bool  CheckUserByToken(IToken token)
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            ISecurityCacheProvider sc 
= SecurityCacheFactory.GetSecurityCacheProvider("Cache Manager");
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//得到Token对应的身份IIdentity
Enterprise Library 2.0 -- Security Application Block
            IIdentity savedIdentity = sc.GetIdentity(token);
Enterprise Library 2.0 -- Security Application Block            
//返回该身份是否已被授权
Enterprise Library 2.0 -- Security Application Block
            return savedIdentity.IsAuthenticated;
Enterprise Library 2.0 -- Security Application Block            
Enterprise Library 2.0 -- Security Application Block        }
   

3、结束User Session(即使Token过期)

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block         /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 使用户Token过期
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>

Enterprise Library 2.0 -- Security Application Block         public   void  CloseUserSession()
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            ISecurityCacheProvider sc 
= SecurityCacheFactory.GetSecurityCacheProvider("Cache Manager");
Enterprise Library 2.0 -- Security Application Block            
//保存身份
Enterprise Library 2.0 -- Security Application Block
            IToken token = sc.SaveIdentity(new GenericIdentity("SHY520"));
Enterprise Library 2.0 -- Security Application Block            
//使身份过期
Enterprise Library 2.0 -- Security Application Block
            sc.ExpireIdentity(token);
Enterprise Library 2.0 -- Security Application Block        }

4、判断用户是否满足一定的规则
      首先需要导入下面的命名空间:
 using System.Security;
 using System.Security.Principal;
 using Microsoft.Practices.EnterpriseLibrary.Security;

Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block     /// <summary>
Enterprise Library 2.0 -- Security Application Block        
/// 判断用户是否满足一定的规则
Enterprise Library 2.0 -- Security Application Block        
/// http://pw.cnblogs.com
Enterprise Library 2.0 -- Security Application Block        
/// </summary>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="username">用户名称</param>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="role">角色名称</param>
Enterprise Library 2.0 -- Security Application Block        
/// <param name="rule">规则名称</param>
Enterprise Library 2.0 -- Security Application Block        
/// <returns></returns>

Enterprise Library 2.0 -- Security Application Block         public   bool  CheckUserRoleByRule( string  username,  string  role,  string  rule)
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block            IPrincipal principal 
= new GenericPrincipal(new GenericIdentity(username), new string[] { role });
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//创建一个默认的Authorization实例
Enterprise Library 2.0 -- Security Application Block
            IAuthorizationProvider autoprovider = AuthorizationFactory.GetAuthorizationProvider();
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block            
//返回验证是否通过
Enterprise Library 2.0 -- Security Application Block
            return autoprovider.Authorize(principal, rule);
Enterprise Library 2.0 -- Security Application Block        }

通过上面的方法可以很简单的实现对用户权限的鉴定,关于Security Application Block的简单应用就说到这里,下面我们来看一下如何实现自定义的authorization provider。
1、首先我们建立自己的Provider类,该类需继承AuthorizationProvider;
2、并且要加上[ConfigurationElementType(typeof(CustomAuthorizationProviderData))]的Attribute;
3、添加构造函数,参数为NameValueCollection类型;
4、覆盖基类中的Authorize方法,在其中添加自己的验证是否授权的逻辑
这样就完成了一个自定义的authorization provider。
完整的类定义如下:

Enterprise Library 2.0 -- Security Application Blockusing  System;
Enterprise Library 2.0 -- Security Application Block
using  System.Collections.Generic;
Enterprise Library 2.0 -- Security Application Block
using  System.Text;
Enterprise Library 2.0 -- Security Application Block
using  Microsoft.Practices.EnterpriseLibrary.Security;
Enterprise Library 2.0 -- Security Application Block
using  System.Collections.Specialized;
Enterprise Library 2.0 -- Security Application Block
using  System.Security.Principal;
Enterprise Library 2.0 -- Security Application Block
using  Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
Enterprise Library 2.0 -- Security Application Block
using  Microsoft.Practices.EnterpriseLibrary.Security.Configuration;
Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block
namespace  Enterprise_Library_2
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block
{
Enterprise Library 2.0 -- Security Application Block    [ConfigurationElementType(
typeof(CustomAuthorizationProviderData))]
Enterprise Library 2.0 -- Security Application Block    
public class CustomProvider : AuthorizationProvider
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block    
{
Enterprise Library 2.0 -- Security Application Block        
public CustomProvider(NameValueCollection configurationItems)
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            
Enterprise Library 2.0 -- Security Application Block        }

Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block        
public override bool Authorize(IPrincipal principal, string context)
Enterprise Library 2.0 -- Security Application BlockEnterprise Library 2.0 -- Security Application Block        
{
Enterprise Library 2.0 -- Security Application Block            
//TODO:在此处添加自己需要的验证授权的逻辑既可
Enterprise Library 2.0 -- Security Application Block

Enterprise Library 2.0 -- Security Application Block            
return true;
Enterprise Library 2.0 -- Security Application Block        }

Enterprise Library 2.0 -- Security Application Block
Enterprise Library 2.0 -- Security Application Block    }

Enterprise Library 2.0 -- Security Application Block}

关于Security Application Block的介绍就说到这里了,有疏忽错误的地方,还请指正,希望对初学者有所帮助!

Email:pwei013@163.com