Fork me on GitHub
微软企业库5.0学习笔记(十三)

开发人员经常编写需要安全功能的应用程序。这些应用程序通常需要执行一系列不同的安全操作,而且它们还经常与不同的基础安全提供程序(如 Microsoft Active Directory 目录服务、授权管理器、Active Directory 应用程序模式 (ADAM) 和自定义数据库等)进行交互。


    安全应用程序块通过收集开发人员必须执行的许多最常见的安全任务,来简化开发人员的工作。每个任务都以一致的方式处理,从特定的安全提供程序中抽象出应用 程序代码并使用最佳做法。您甚至可以通过更改配置来更改基础提供程序,而无需更改基础应用程序代码。

 DemoDownLoad

验证应用程序块功能框架如下图所示:

 

几个基本概念: 

 

(1) Ticket提供对票证的属性和值的访问,这些票证用于Forms身份验证,对用户进行标识。可以使用FormsIdentity 类的 Ticket 属性访问当前经过身份验证的用户的 FormsAuthenticationTicket。通过将当前User 的 Identity 属性强制转换为类型 FormsIdentity,可以访问当前 FormsIdentity 对象。

  

(2) Token与当前执行线程关联的访问标记的句柄,用于获取用户的Windows帐户标记。通常,通过调用非托管代码(如调用 Win32 API LogonUser 函数)来检索该帐户标记。 

 

(3) IdentityIdentity 封装有关正在验证的用户或实体的信息。在最基本的级别上,Identity包含名称和身份验证类型。 名称可以是用户名或 Windows 帐户名,而身份验证类型可以是所支持的登录协议(如 Kerberos V5)或自定义值。.NET Framework 定义了一个 GenericIdentity 对象和一个更专用的 WindowsIdentity 对象;前者可用于大多数自定义登录方案,而后者可用于在希望应用程序依赖于 Windows 身份验证的情况中。此外,您还可以定义自己的标识类来封装自定义用户信息。 

 

(4) PrincipalPrincipal 表示代码运行时所在的安全上下文。实现基于角色的安全性的应用程序将基于与主体对象 关联的角色来授予权限。同标识对象类似,.NET Framework 提供 GenericPrincipal 对象和 WindowsPrincipal 对象。您还可以定义自己的自定义主体类。

 

 

下面介绍如何使用Microsoft Enterprise Library 5.0中的验证应用程序模块.

<!--[if !supportLists]-->1.       <!--[endif]-->运行EntLibConfig.exe,选择Blocks菜单 ,单击 Add Database Settings .

 

 

 

<!--[if !supportLists]-->2.       <!--[endif]-->点击Authorization Providers  区块右上角的加号按钮, Add Authorization Providers然后点击 Add Authorization  Rule Provider  :

 


<!--[if !vml]-->

<!--[endif]-->

<!--[if !supportLists]-->3.       <!--[endif]-->在Authorition Rule Provider面板上右键,点击Add Authorization Rule,我们将新建的Rule名称设置为GetAllCollege,表示只有符合验证表达式的用户或角色才被允许执行获取所以College操作的权限:

 

<!--[endif]-->

 

<!--[if !supportLists]-->4.       <!--[endif]-->点击Rule Expression右边的按钮,弹出验证表达式编辑对话框:


<!--[if !vml]-->

<!--[endif]-->

<!--[if !supportLists]-->5.       <!--[endif]-->在Authorition Rule Provider面板上右键,点击Add Authorization Rule,添加一个凭据缓存:

<!--[if !vml]-->

<!--[endif]-->

 

<!--[if !supportLists]-->6.       <!--[endif]-->点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它.

 

<!--[if !supportLists]-->7.       <!--[endif]-->创建一个新的控制台应用程序,将App.config添加到程序内,并加入需要的Dll文件,在此我们要导入的是 Microsoft.Practices.EnterpriseLibrary. Security.dll, Microsoft.Practices.EnterpriseLibrary. Security.Cache.CachingStore.dll并添加需要的引用:

<!--[if !vml]--><!--[endif]-->

 

 

 

<!--[if !supportLists]-->8. 测试:

<!--[if !supportLists]-->

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using Microsoft.Practices.EnterpriseLibrary.Security;

namespace test
{
class Program
{
static void Main(string[] args)
{
//创建一个新的用户
GenericIdentity gID = new GenericIdentity("huang");

IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider("Authorization Rule Provider");

//设置该用户隶属于Manage中
IPrincipal principal = new GenericPrincipal(gID, new string[] { "Manage" });

//验证
bool authorized = ruleProvider.Authorize(principal, "GetAllCollege");

if (authorized)
{
Console.WriteLine("验证成功!");
//保存用户至缓存中
ISecurityCacheProvider secCache = SecurityCacheFactory.GetSecurityCacheProvider("Security Cache");

//保存,并获取一个凭证
IToken token = secCache.SaveIdentity(gID);

//通过凭证获取缓存中的用户
IIdentity savedIdentity = secCache.GetIdentity(token);

//打印用户信息
Console.WriteLine(savedIdentity.Name);
}
else
{
Console.WriteLine("登录失败!");
}
}
}
}

 

 

 

 

9.       <!--[endif]-->运行结构:

posted on 2010-06-03 14:28  HackerVirus  阅读(495)  评论(0编辑  收藏  举报