Enterprise Library Step By Step系列(五):安全应用程序块——入门篇
2005-10-25 11:55
573 查看
写在前面:按照本系列随笔的风格,还是立足于初学者,所以在入门篇里面介绍的比较简单,在进阶篇里面多介绍一些进一步的探索内容。避免给初学者一种觉得安全应用程序块很复杂的感觉。
一. 安全应用程序块概述:
安全应用程序块通过一个或多个安全机制,帮助开发人员在应用程序中实现通用的安全相关任务。
需要提高扩展性,以便在不改变应用程序代码的情况下更改认证或授权方式。
提供了以下几个方面的功能:
1.认证
2.授权
3.角色管理
4.Profile管理
二. 几个重要的概念:
1. Credential(令牌)
2. Identity(身份)
3. Principal(主体特征)
三. 使用三部曲:
在进行使用前请确保项目中有App.config或Web.config文件,并且需要数据库中有相关的表和存储过程,具体可参见数据库EntLibQuickStarts(Enterprise 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,并设置SaltEnabled为True;
(6)选择Security Application Block | Authentication | Database Provider,设置 Database属性为 Database Instance, 并且设置 HashProvider属性为SHA1Managed;
(7)选择Security Application Block,设置它的DefaultAuthenticationInstance为Database Provider;
copy "$(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,包括第三方的基于数据库角色授权的插件等。
一. 安全应用程序块概述:
安全应用程序块通过一个或多个安全机制,帮助开发人员在应用程序中实现通用的安全相关任务。
需要提高扩展性,以便在不改变应用程序代码的情况下更改认证或授权方式。
提供了以下几个方面的功能:
1.认证
2.授权
3.角色管理
4.Profile管理
二. 几个重要的概念:
1. Credential(令牌)
2. Identity(身份)
3. Principal(主体特征)
三. 使用三部曲:
在进行使用前请确保项目中有App.config或Web.config文件,并且需要数据库中有相关的表和存储过程,具体可参见数据库EntLibQuickStarts(Enterprise 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,并设置SaltEnabled为True;
(6)选择Security Application Block | Authentication | Database Provider,设置 Database属性为 Database Instance, 并且设置 HashProvider属性为SHA1Managed;
(7)选择Security Application Block,设置它的DefaultAuthenticationInstance为Database Provider;
copy "$(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,包括第三方的基于数据库角色授权的插件等。
相关文章推荐
- Enterprise Library Step By Step系列(五):安全应用程序块——入门篇
- Enterprise Library Step By Step系列(五):安全应用程序块——入门篇
- Enterprise Library Step By Step系列(六):安全应用程序块——进阶篇
- Enterprise Library Step By Step系列(六):安全应用程序块——进阶篇
- Enterprise Library Step By Step系列(六):安全应用程序块——进阶篇
- Enterprise Library Step By Step系列(五):安全应用程序块——入门篇
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Enterprise Library Step By Step系列(三):数据访问程序块——入门篇
- Enterprise Library Step By Step系列(一):配置应用程序块——入门篇
- Enterprise Library Step By Step系列(七):日志和监测应用程序块——入门篇
- Enterprise Library Step By Step系列(四):数据访问程序块——进阶篇
- Enterprise Library Step By Step系列(七):日志和监测应用程序块——入门篇
- Enterprise Library Step By Step系列(七):日志和监测应用程序块——入门篇
- Enterprise Library Step By Step系列(十三):加密应用程序块——入门篇
- Enterprise Library Step By Step系列(四):数据访问程序块——进阶篇
- Enterprise Library Step By Step系列(四):数据访问程序块——进阶篇
- Enterprise Library Step By Step系列(四):数据访问程序块——进阶篇