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

news/2024/7/7 17:09:18

练习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按钮。

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.设置属性NameBugSmak 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的源代码,添加如下代码:

None.gif//[PrincipalPermission(SecurityAction.Demand, Role = "Employee")]
None.gif
None.gif
//[PrincipalPermission(SecurityAction.Demand, Role = "Developer")]
None.gif
None.gif
//[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
None.gif

None.gif
public static RaiseBug Create()
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// TODO: Check Authorization
InBlock.gif

InBlock.gif    
if (!SecurityHelper.Authorized(AuthRule.Raise))
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
throw new SecurityException();
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return new RaiseBug();
InBlock.gif
ExpandedBlockEnd.gif}

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

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

Microsoft.Practices.EnterpriseLibrary.Security.dll

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

None.gifusing Microsoft.Practices.EnterpriseLibrary.Security;

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

None.gifpublic static bool Authorized(string rule)
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
bool authorized = false;
InBlock.gif
InBlock.gif    
// TODO: Check rule-base authorization
InBlock.gif

InBlock.gif    IAuthorizationProvider ruleProvider;
InBlock.gif
InBlock.gif    ruleProvider 
= AuthorizationFactory.GetAuthorizationProvider();
InBlock.gif
InBlock.gif    authorized 
= ruleProvider.Authorize(Thread.CurrentPrincipal, rule);
InBlock.gif
InBlock.gif    
return authorized;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif


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系列文章

 

转载于:https://www.cnblogs.com/Terrylee/archive/2006/10/14/Security_Application_Block_HandsOnLab_Part2.html


http://lihuaxi.xjx100.cn/news/255237.html

相关文章

LeetCode实战:不同路径

题目英文 A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Fini…

[bzoj4562][Haoi2016]食物链_记忆化搜索_动态规划

食物链 bzoj-4562 Haoi-2016 题目大意:给你n个点,m条边的DAG,求所有的满足条件的链,使得每条链的起点是一个入度为0的点,中点是一条出度为0的点。 注释:$1\le n\le 10^5$,$1\le m\le 2*10^5$。 …

数组与纠结的排序篇

数组之纠结的排序 1.数组是什么? 数组:所谓数组,就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,…

接口性能优化技巧,有点硬...

欢迎关注方志朋的博客,回复”666“获面试宝典文章来源:http://b.nxw.so/1jSSgg目录背景哪些问题会引起接口性能问题问题解决总结背景我负责的系统在去年初就完成了功能上的建设,然后开始进入到推广阶段。随着推广的逐步深入,收到了…

基于OHCI的USB主机 —— 结束语

从去年11月份开始连载的《基于OHCI的USB主机》系列总算告一段落了,到UFI命令层为止,所有USB主机的底层处理就结束了,再上面就是磁盘读写、文件系统、文件读写和应用系统了。这些上层应用基本上是与USB主机没有什么关系的,我的这个…

GPT-3 再更新,新增编辑和插入文本功能,简直不要太好用!

编译 | 禾木木出品 | AI科技大本营(ID:rgznai100)GPT-3 是 OpenAL 提出的基于上下文的超大规模自然处理深度学习模型。这意味着如果你给 GPT-3 某些上下文内容时,它会试图去填充其余内容。例如给出句子的前部分,它会推测出下半部分…

LeetCode实战:合并两个有序数组

题目英文 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and nums2 are m and n respectively.You may assume that nums1 has enough space (size that is greater o…

人工智能本科专业高校名单大全(440所)

Datawhale干货 编辑:机器之心、Datawhale文章有点长,目录预览:清华大学刘知远教授答疑各大开设人工智能的院校在计算机专业和人工智能日益火爆的当下,很多人对这两个专业又是好奇又是憧憬。对此,清华大学刘知远教授近日…