Enterprise Library - Security Application Block 学习手册(最新版) Part 2

Enterprise Library - Security Application Block 学习手册(最新版) Part 2
 
本系列文章引导学习Enterprise Library - Security Application Block,并实践如何在不同的应用程序环境中使用Security Application Block的功能。前一节中,使用基于角色的授权来保护应用程序,这一节将练习使用AuthorizationProvider来保护应用程序。本文由http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib HOL手册编译提供,欢迎交流。
 
使用AuthorizationProvider实现如下功能:
(1)映射基于任务的授权为复杂的角色组合;
(2)从应用程序代码中提取授权;
 
练习二:在应用程序中使用基于规则的授权(Rule-Based Authorization
首先打开ex02\begin 目录下的BugSmak.sln 项目文件。
 
使用Enterprise Library配置管理工具
1. 使用Enterprise Library配置工具打开app.config配置文件,添加新的Security Application Block配置项。
 
 
2. 选择Security Application Block | Authorization 节点,添加一个新的Authorization Rule Provider。
 
 
设置Name =BugSmak Rules。
 
3. 选择 Security Application Block | Authorization | BugSmak Rules 节点,通过New | Rule菜单项添加新的Rule。
 
 
4. 选择Security Application Block | Authorization | BugSmak Rules | Rule 节点,点击Expression属性后的… 按钮,弹出Rule Expression Editor对话框。
 
 
在Rule Expression Editor对话框中,设置如下属性:
Rule Name = Raise Bug
Expression = R:Developer OR R:Employee OR R:Manager
 
 
用户必须是Developer、Employee、或Manager角色。
 
5. 按照上述步骤,添加新的Rule和表达式。
规则名称:Assign Bug   --- 表达式Expression:R:Manager
规则名称:Resolve Bug   --- 表达式Expression:R:Developer OR R:Manager
 
设置完成后,界面如下图所示。
 
 
6. 选择Security Application Block节点,设置如下属性。
DefaultAuthorizationInstance = BugSmak Rules
 
最后保存好app.config配置文件。
 
为应用程序添加基于任务的授权
1. 打开TaskForms \ RaiseBug.cs代码文件,其中关于PrincipalPermissions部分的硬编码已经注释掉了,这里将采用更加灵活的任务授权。
        //[PrincipalPermission(SecurityAction.Demand, Role = "Employee")]
        //[PrincipalPermission(SecurityAction.Demand, Role = "Developer")]
        //[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
        public static RaiseBug Create()
        {
            // TODO: Check Authorization
            if (!SecurityHelper.Authorized(AuthRule.Raise))
            {
                throw new SecurityException();
            }
 
            return new RaiseBug();
        }
AssignBug.cs 和 ResolveBug.cs 有上述相似的代码。
 
2. 为项目文件添加对Security Application Block程序集的引用。
Microsoft.Practices.EnterpriseLibrary.Security.dll
 
3. 打开Security \ SecurityHelper.cs代码文件,添加如下命名空间的引用。
using Microsoft.Practices.EnterpriseLibrary.Security;
 
4. 添加如下代码到Authorized方法。
        public static bool Authorized(string rule)
        {
            bool authorized = false;
 
            // TODO: Check rule-base authorization
            IAuthorizationProvider ruleProvider;
            ruleProvider = AuthorizationFactory.GetAuthorizationProvider();
 
            authorized = ruleProvider.Authorize(Thread.CurrentPrincipal, rule);
 
            return authorized;
        }
 
上述代码使用AuthorizationFactory 检索默认的authorization provider,授权提供者将依据规则,测试当前的principal( role和identity)是否授权。
 
5. 运行应用程序,分别以不同的用户Tom / Dick / Harry登录,验证不同的用户是否分配了不同的权限。
http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问Security  Application Block学习手册。
 
参考文档:
Security Application Block Hands-On Labs for Enterprise Library
 

 

posted @ 2009年10月12日 19:53 | entlibforum 阅读 (0) | 评论 (0)

Enterprise Library - Security Application Block 学习手册(最新版) Part 1

Enterprise Library - Security Application Block 学习手册(最新版) Part 1
 
本系列文章引导学习Enterprise Library - Security Application Block,并实践如何在不同的应用程序环境中使用Security Application Block的功能。本文由http://blog.entlib.com 开源ASP.NET博客平台小组根据EntLib HOL手册编译提供,欢迎交流。
 
练习一:保护应用程序
首先打开ex01\begin 目录下的BugSmak.sln 项目文件。
 
为应用程序添加认证功能
运行应用程序,此时应用程序尚不能对用户进行认证。
 
1. 打开Security \ SecurityHelper.cs代码文件,添加如下命名空间的引用。
using System.Web.Security;
 
2. 添加如下代码到Authenticate 方法。
        public static bool Authenticate(string username, string password)
        {
            bool authenticated = false;
 
            authenticated = Membership.ValidateUser(username, password);
 
            // TODO: Get Roles
 
            return authenticated;
        }
LoginForm调用Authenticate方法对用户进行验证。Membership.ValidateUser方法执行验证操作。Membership system使用Provider 模式,因此应用程序不会绑定到特定的数据。ASP.NET 提供了2个membership provider:一个是使用Microsoft SQL Server作为数据源,另一个则使用Windows Active Directory。
也可以创建定制的membership provider,范例程序将从XML文件中读取应用程序的成员信息。
 
3. 打开项目中的Security | Providers | ReadOnlyXmlMembershipProvider.cs代码文件。
ReadOnlyXmlMembershipProvider继承MembershipProvider,是一个定制的membership provider,读取一个未加密的XML文件。虽然这不是一个好的设计,但对本练习而言是合适的。
 
4. 打开项目中的app.config 配置文件,检查membership provider 配置,认证数据源设定为Users.xml文件。
    <membershipdefaultProvider="ReadOnlyXmlMembershipProvider">
      <providers>
        <addname="ReadOnlyXmlMembershipProvider"
             type="BugSmak.Security.Providers.ReadOnlyXmlMembershipProvider, BugSmak"
             description="Read-only XML membership provider"
             xmlFileName="Users.xml" />
      </providers>
    </membership>
一旦你有定制的membership provider,你可以和配置ASP.NET provider一样,在应用程序中配置使用定制的membership provider。
 
5. 打开Users.xml文件,可以看到已经运行添加了Tom / Dick / Harry 等等用户。
 
6. 现在运行范例程序,以Tom / Dick / Harry 可以验证通过,并登录。
本范例程序是一个简单的bug跟踪系统的框架,提供了如下功能点:提出Bug,分配Bug给开发人员,解决Bug。
 
 
7. 以Tom用户登录,选择 Tasks | Raise New Bug 菜单项,将看如下提示信息:
Sorry, you aren’t allowed to access that form.
同样地,在访问Assign Bug 和 Resolve Bug是也会出现上述提示信息,退出应用程序。
 
为应用程序添加基于角色的授权
1. 打开TaskForms \ RaiseBug.cs文件,查看代码。
RaiseBug窗口需要用户有Developer、Employee、或Manager角色之一。
        [PrincipalPermission(SecurityAction.Demand, Role = "Employee")]
        [PrincipalPermission(SecurityAction.Demand, Role = "Developer")]
        [PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
        public static RaiseBug Create()
        {
            // TODO: Check Authorization
 
            return new RaiseBug();
        }
 
如果不具备必要的权限,将抛出SecurityException异常,由MainForm主窗体捕获。
用户认证以及实现了,用户角色还没有实现,在后面的内容中,应用程序将配置使用RoleProvider来检索用户角色。
 
2. 打开Security \ SecurityHelper.cs 代码文件,为 Authenticate方法添加如下代码。
        public static bool Authenticate(string username, string password)
        {
            bool authenticated = false;
 
            authenticated = Membership.ValidateUser(username, password);
 
            if (!authenticated)
                return false;
 
            IIdentity identity;
            identity = new GenericIdentity(username, Membership.Provider.Name);
 
            string[] roles = Roles.GetRolesForUser(identity.Name);
            IPrincipal principal = new GenericPrincipal(identity, roles);
 
            // Place user's principal on the thread
            Thread.CurrentPrincipal = principal;
 
            return authenticated;
        }
通过定制的RoleProvider(ReadOnlyXmlRoleProvider.cs)从Users.xml文件中检索用户角色,并创建一个新的principal,包含用户的identity和角色。
 
3. 打开app.config配置文件,查看role manager provider的配置,指定的数据源为Users.xml文件。
    <roleManagerenabled="true"
                 defaultProvider="ReadOnlyXmlRoleProvider">
      <providers>
        <addname="ReadOnlyXmlRoleProvider"
             type="BugSmak.Security.Providers.ReadOnlyXmlRoleProvider, BugSmak"
             description="Read-only XML role provider"
             xmlFileName="Users.xml" />
      </providers>
    </roleManager>
 
4. 运行范例程序
分别以不同的用户Tom / Dick / Harry依次登录,确认不同用户有不同的访问权限。
Tom (Employee) -- Raise new bug
Dick (Developer) -- Raise new bug and resolve bug
Harry (Manager) -- Raise new bug, resolve bug, and assign bug
 

 
http://www.entlib.com专业ASP.NET电子商务平台小组,欢迎你继续访问Security  Application Block学习手册。
 
参考文档:
Security Application Block Hands-On Labs for Enterprise Library
 

 

posted @ 2009年10月12日 19:38 | entlibforum 阅读 (0) | 评论 (1)
«十月»
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567