且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)

更新时间:2022-08-24 08:16:54

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)练习2:异常处理策略
通过本练习将学会使用异常处理应用程序块的包装策略,来处理一些带有敏感信息的异常。
 
第一步
打开Puzzler2.sln 项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Exception Handling\exercises\ex02,并编译。
 
第二步 保护服务中'Add Word'函数的代码访问安全
1.在解决方案管理器选择项目PuzzlerService中的Dictionary.cs文件,选择View | Code菜单命令,为方法Add Word加上代码访问安全特性。
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)// TODO: Add security attribute
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)[PrincipalPermission(SecurityAction.Demand, Role 
= "Grand PoohBah")]
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
public static Boolean AddWord(string wordToAdd)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
{
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    
if (!IsWord(wordToAdd))
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    
{
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)        
// It is not alphabetic! Throw an exception
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)        
throw new ApplicationException(
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)            
"Word to add does not consist of alphabetic letters");
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    }

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    
if (Dict[wordToAdd] == null)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    
{
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)        Dict.Add(wordToAdd, wordToAdd);
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    }

Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)    
return true;
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)}
现在该方法只可以被角色Grand PoohBah所执行。注意要修改的方法在Dictionary.cs中而不是DictionaryService.cs
2.选择Debug | Start Without Debugging菜单命令运行应用程序。在Word to check文本框中输入数字并单击Add Word按钮。这将会引发服务的AddWord方法抛出一个SecurityException异常信息,在事件查看器中可以看到。
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)这里SecurityException信息从服务端传到了客户端,其中包含的信息将会有助于攻击者来攻破我们的系统安全。所以应该是在服务端记录异常信息,而只发送很少的信息到客户端。
3.关闭应用程序。
 
第三步 配置应用程序包装SecurityExceptions
1.在解决方案管理器中选中App.config文件,在View菜单或者在右键菜单中选择Open With…,将打开OpenWith对话框,选中Enterprise Library Configuration并单击OK按钮。
App.config已经包含了一个空的策略Service Policy,默认的,如果一个策略是空的,异常信息将会从Catch块中重新抛出,事实上该策略并没有做任何事情。
2.选中Service Policy节点,选择Action | New | Exception Type菜单命令。在弹出的对话框中选择System.Security.SecurityException,并单击OK按钮。
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)3.选中Service Policy | SecurityException节点,并设置如下属性。
PostHandlingAction = ThrowNewException
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)4.添加新的Logging Handler,选中Service Policy | SecurityException节点,并选择Action | New | Logging Handler菜单命令,设置如下属性。
FormatterType = TextExceptionFormatter,
LogCategory = General
Title = Security Exception in Service Layer
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)5.添加新的Replace Handler,选中Service Policy | SecurityException节点,选择Action | New | Replace Handler菜单命令,并设置如下属性。
ExceptionMessage = Unauthorized Access
ReplaceExceptionType = System.Security.SecurityException (from mscorlib)
 
第四步 发生安全异常时退出应用程序
1.选中UI Policy节点,选择Action | New | Exception Type菜单命令,选择System.Security.SecurityException类型并单击OK按钮,设置如下属性。
PostHandlingAction = NotifyRethrow
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)处理行为NotifyRethrow将引发Application.ThreadException重新抛出异常,重新抛出的异常将会为一个未处理异常unhandled exception
2.为SecurityExceptionUI Policy策略下面添加新的Logging Handler,选择Action | New | Logging Handler菜单命令,设置如下属性。
FormatterType = TextExceptionFormatter,
LogCategory = General
Title = Security Exception in UI Layer
Enterprise Library 2.0 Hands On Lab 翻译(8):异常应用程序块(二)3.保存应用程序配置并关闭Enterprise Library Configuration工具。
 
第五步 测试包装处理
选择Debug | Start Without Debugging运行应用程序,在Word to check文本框中输入数字并单击Add Word按钮。如果单击Continue将会出现“Unhandled exception”异常信息,这时不会再出现安全异常信息,可以打开事件查看器查看相关的日志记录情况。
在这里事件日志将会记录三次异常,第一次是由服务层引发的Service Policy所记录的异常,这是记录在服务端的;第二次是Application ThreadException引发的UI Policy所记录的异常,它是记录在客户端,由于这里服务端和客户端使用的是同一个机器;第三次是AppDomain UnhandledException引发的Unhandled Policy记录的异常。
 
注意根据Hands On Lab给出的时间建议,做完以上两个练习的时间应该为30分钟。
 
更多Enterprise Library的文章请参考《Enterprise Library系列文章
















本文转自lihuijun51CTO博客,原文链接:http://blog.51cto.com/terrylee/67639 ,如需转载请自行联系原作者