Enterprise Library 2.0 hands on Lab translation (8): Exception Application Block (ii)

Source: Internet
Author: User
Tags exception handling

Exercise 2: Exception handling policies

This exercise will learn to use the wrapper policy of the exception handling application block to handle some exceptions with sensitive information.

First step

Open the Puzzler2.sln project, the default installation path should be C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\exception EXERCISES\EX02, and compiled.

Step two protects code access security for the ' Add Word ' function in the service

1. In Solution Manager Select the Dictionary.cs file in the project Puzzlerservice, select View | The Code menu command to add Word for method add to access security attributes.

// 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;
}

The method can now only be executed by the role Grand Poohbah. Note that the method to be modified is in Dictionary.cs instead of DictionaryService.cs.

2. Select Debug | The Start without Debugging menu command runs the application. Enter a number in the Word to check text box and click the Add Word button. This will cause the service's Addword method to throw a SecurityException exception message that can be seen in Event Viewer.

Here SecurityException information is uploaded from the server to the client, and the information contained therein will help the attacker to compromise our system security. So you should log exception information on the server and send only a small amount of information to the client.

3. Closes the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.