zoukankan      html  css  js  c++  java
  • 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加上代码访问安全特性。

    // TODO: Add security attribute

    [PrincipalPermission(SecurityAction.Demand, Role 
    = "Grand PoohBah")]

    public static Boolean AddWord(string wordToAdd)

    {

        
    if (!IsWord(wordToAdd))

        
    {

            
    // It is not alphabetic! Throw an exception

            
    throw new ApplicationException(

                
    "Word to add does not consist of alphabetic letters");

        }


        
    if (Dict[wordToAdd] == null)

        
    {

            Dict.Add(wordToAdd, wordToAdd);

        }


        
    return true;

    }

    现在该方法只可以被角色Grand PoohBah所执行。注意要修改的方法在Dictionary.cs中而不是DictionaryService.cs

    2.选择Debug | Start Without Debugging菜单命令运行应用程序。在Word to check文本框中输入数字并单击Add Word按钮。这将会引发服务的AddWord方法抛出一个SecurityException异常信息,在事件查看器中可以看到。

    这里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按钮。

    3.选中Service Policy | SecurityException节点,并设置如下属性。

    PostHandlingAction = ThrowNewException

    4.添加新的Logging Handler,选中Service Policy | SecurityException节点,并选择Action | New | Logging Handler菜单命令,设置如下属性。

    FormatterType = TextExceptionFormatter,

    LogCategory = General

    Title = Security Exception in Service Layer

    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

    处理行为NotifyRethrow将引发Application.ThreadException重新抛出异常,重新抛出的异常将会为一个未处理异常unhandled exception

    2.为SecurityExceptionUI Policy策略下面添加新的Logging Handler,选择Action | New | Logging Handler菜单命令,设置如下属性。

    FormatterType = TextExceptionFormatter,

    LogCategory = General

    Title = Security Exception in UI Layer

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

  • 相关阅读:
    POJ 3268 Silver Cow Party (Dijkstra)
    怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
    CF Amr and Music (贪心)
    CF Amr and Pins (数学)
    POJ 3253 Fence Repair (贪心)
    POJ 3069 Saruman's Army(贪心)
    POJ 3617 Best Cow Line (贪心)
    CF Anya and Ghosts (贪心)
    CF Fox And Names (拓扑排序)
    mysql8.0的新特性
  • 原文地址:https://www.cnblogs.com/Terrylee/p/Exception_Handling_Application_Block_HandsOnLab_Part2.html
Copyright © 2011-2022 走看看