Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
练习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 name为Enterprise Library Configuration,单击OK按钮。
Visual Studio会把配置文件(App.config)作为一个命令行参数传递给EntLibConfig.exe。
4.在Open With对话框中,选中Enterprise Library Configuration并单击OK按钮。
第三步 使用企业库配置工具添加授权规则
1.在应用程序上右击并选择New | Security Application Block。
2.添加新的Authorization Rule Provider。选中Security Application Block | Authorization节点,选择Action | New | Authorization Rule Provider菜单命令。
3.设置属性Name为BugSmak Rules。
4.选中Security Application Block | Authorization | BugSmak Rules节点,选择Action | New | Rule菜单命令。
5.在Expression属性上点击ellipsis打开角色表达式编辑器。
6.设置如下属性并单击Ok按钮
Rule Name = Raise Bug
Expression = R:Developer OR R:Employee OR R:Manager
用户必须在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 |
8.选择Security Application Block节点并设置如下属性:
DefaultAuthorizationInstance = BugSmak Rules
第四步 添加基于授权的任务
1.打开文件TaskForms \ RaiseBug.cs的源代码,添加如下代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
其他两个窗体AssignBug.cs和ResolveBug.cs添加的代码类似。
2.添加对如下程序集的引用。
Microsoft.Practices.EnterpriseLibrary.Security.dll
3.打开文件Security \ SecurityHelper.cs,添加如下命名空间。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.在方法Authorized中添加如下代码。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5.运行应用程序,分别以Tom, Dick和Harry登录,测试他们的访问权限。
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系列文章》
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com