练习2:在应用程序中使用基于角色的授权

通过该练习将学习使用AuthorizationProvider在应用程序中进行基于角色的授权。

 

第一步

BugSmak.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Security\exercises\ex02\begin,并编译。

 

第二步 企业库配置工具

1.使用Enterprise Library配置工具配置应用程序,可以通过开始菜单打开该配置工具,选择所有程序| Microsoft patterns and practices | Enterprise Library | Enterprise Library Configuration,并打开App.config文件。或者直接在Visual Studio中使用该工具打开配置文件。

2.在解决方案管理器中选中App.config文件,在View菜单或者在右键菜单中选择Open With…,将打开OpenWith对话框,单击Add按钮。

3.在Add Program对话框中,设置Program name指向EntLibConfig.exe文件,默认的路径为C:\Program Files\Microsoft Enterprise Library January 2006\bin,设置Friendly nameEnterprise Library Configuration,单击OK按钮。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

Visual Studio会把配置文件(App.config)作为一个命令行参数传递给EntLibConfig.exe

4.在Open With对话框中,选中Enterprise Library Configuration并单击OK按钮。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

 

第三步 使用企业库配置工具添加授权规则

1.在应用程序上右击并选择New | Security Application Block

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

2.添加新的Authorization Rule Provider。选中Security Application Block | Authorization节点,选择Action | New | Authorization Rule Provider菜单命令。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

3.设置属性NameBugSmak Rules

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

4.选中Security Application Block | Authorization | BugSmak Rules节点,选择Action | New | Rule菜单命令。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

5.在Expression属性上点击ellipsis打开角色表达式编辑器。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

6.设置如下属性并单击Ok按钮

Rule Name = Raise Bug

Expression = R:Developer OR R:Employee OR R:Manager

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

用户必须在Developer, Employee或者 Manager角色中。

7.分别添加如下规则:

Rule Name

Expression

Raise Bug **

R:Developer OR R:Employee OR R:Manager

Assign Bug

R:Manager

Resolve Bug

R:Developer OR R:Manager

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

8.选择Security Application Block节点并设置如下属性:

DefaultAuthorizationInstance = BugSmak Rules

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

 

第四步 添加基于授权的任务

1.打开文件TaskForms \ RaiseBug.cs的源代码,添加如下代码:

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)//[PrincipalPermission(SecurityAction.Demand, Role = "Employee")]
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
//[PrincipalPermission(SecurityAction.Demand, Role = "Developer")]
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
//[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
public static RaiseBug Create()
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
}

其他两个窗体AssignBug.csResolveBug.cs添加的代码类似。

2.添加对如下程序集的引用。

Microsoft.Practices.EnterpriseLibrary.Security.dll

3.打开文件Security \ SecurityHelper.cs,添加如下命名空间。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)using Microsoft.Practices.EnterpriseLibrary.Security;

4.在方法Authorized中添加如下代码。

Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)public static bool Authorized(string rule)
Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)


5.运行应用程序,分别以Tom, DickHarry登录,测试他们的访问权限。

User

Task Access

Tom (Employee)

Raise New Bug

Dick (Developer)

Raise New Bug

Resolve Bug 

Harry (Manager)

Raise New Bug

Resolve Bug

Assign Bug

6.关闭应用程序。

 

更多Enterprise Library的文章请参考《Enterprise Library系列文章

 

相关文章: