Enterprise Library Step By Step系列(五):安全应用程序块——入门篇

 写在前面:按照本系列随笔的风格,还是立足于初学者,所以在入门篇里面介绍的比较简单,在进阶篇里面多介绍一些进一步的探索内容。避免给初学者一种觉得安全应用程序块很复杂的感觉。

一.     安全应用程序块概述:

安全应用程序块通过一个或多个安全机制,帮助开发人员在应用程序中实现通用的安全相关任务。

需要提高扩展性,以便在不改变应用程序代码的情况下更改认证或授权方式。

提供了以下几个方面的功能:

1.认证

2.授权

3.角色管理

4Profile管理

二.     几个重要的概念:

1.          Credential(令牌)

2.          Identity(身份)

3.          Principal(主体特征)

三.     使用三部曲:

在进行使用前请确保项目中有App.configWeb.config文件,并且需要数据库中有相关的表和存储过程,具体可参见数据库EntLibQuickStartsEnterprise Library的示例库)。

1.定义配置文件:

1)运行Enterprise Library Configuration 配置工具,打开项目中的配置文件;

2)右击Application,选择New | Security Application Block,创建一个安全应用程序块;

3)在Security Application Block | Authentication 节点上右击,选择 New | Database Authentication Provider,创建一个数据库认证Provider

4)根据在数据访问应用程序块中所讲的,设置数据库的连接字符串;

5)右击Cryptography Application Block | Hash Providers,选择New | Hash Algorithm Provider,创建一个加密Provider,并设置SaltEnabledTrue

6)选择Security Application Block | Authentication | Database Provider设置 Database 属性为 Database Instance, 并且设置 HashProvider 属性为SHA1Managed

7)选择Security Application Block,设置它的DefaultAuthenticationInstanceDatabase Provider



8)选择File | Save All保存全部。

至此,关于认证部分的配置已经做完。仅仅有这些还不够,我们还要进行设置角色。

9)选择Security Application Block,右击Roles,选择New | Role Database Provider创建一个角色的数据库Provider

10)设置Database属性为Database Instance

11)选择Security Application Block节点,设置DefaultRolesInstance,为RolesDatabase Provider


12)保存全部File | Save All

至此,所有的配置文件的工作已经做完。最后别忘了,做拷贝目录

1copy "$(ProjectDir)\*.config" "$(TargetDir)"

2.创建Security Provider实例:

1IAuthenticationProvider authprovider;
2            authprovider = AuthenticationFactory.GetAuthenticationProvider();

3.执行Security Provider命令:

 1 public static bool Authenticate(string username, string password)
 2        {
 3            bool authenticated = false;
 4
 5            
 6            NamePasswordCredential credentials;
 7            credentials = new NamePasswordCredential(username, password);
 8 
 9            IAuthenticationProvider authprovider;
10            authprovider = AuthenticationFactory.GetAuthenticationProvider();
11 
12            IIdentity identity;
13            authenticated = authprovider.Authenticate(credentials, out identity);
14 
15            if (!authenticated)
16            {
17                throw new SecurityException("Invalid username or password.");
18            }

19            
20          
21            
22            IRolesProvider rolesprovider;
23            rolesprovider = RolesFactory.GetRolesProvider();
24 
25            IPrincipal principal;
26            principal = rolesprovider.GetRoles(identity);
27 
28            // Place user's principal on the thread
29            Thread.CurrentPrincipal = principal;
30            
31            return authenticated;
32        }

入门篇就到这里了,安全应用程序块内容比较多,所以有些步骤里面我没有截图,请大家谅解。在进阶篇里面,我会分别介绍认证,授权(包括授权规则),角色,个性化服务,以及严格的帐号管理策略,Security Cache,包括第三方的基于数据库角色授权的插件等。

作者:TerryLee
出处:http://terrylee.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2005-10-25 11:55 TerryLee 阅读(6266) 评论(12)  编辑 收藏 网摘 所属分类: [10]  模式与实践

  回复  引用  查看    
#1楼 2005-10-25 21:42 | 置身珠海,学习与奋斗      
在.Net 2.0中,很多东西已经内置了
  回复  引用  查看    
#2楼 2005-10-27 15:02 | 高海东      
认证,授权(包括授权规则)一般都是保存在文件里面
希望您做的时候事例都可以做到数据库里面
  回复  引用    
#3楼 2006-06-10 18:17 | stephen2006 [未注册用户]
注意一点,企业库有变化,部分功能已经用.NET Framework 2.0实现,
我试验了2个小时,不能建立authentication,原来在是Enterprise Library取消了。

The Security Application Block – January 2006 supports user authorization and security-related caching. The functionality that was in earlier releases of the application block that supported authenticating a user and setting and retrieving role and profile information are no longer a part of the application block. Instead, use the .NET Framework 2.0 to implement these features.
  回复  引用  查看    
#4楼 2007-11-28 13:18 | RyanYu      
不是很明白~




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-12-09 16:22 编辑过
Google站内搜索

相关文章:

相关链接: