RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using Enterprise Library in ASP.NET 2.0 Partial Trust Mode : Page 5

The Enterprise Library Application Blocks aren't useful only in Windows Forms applications; you can use them in ASP.NET too by downloading a set of patch files and configuring the security settings appropriately. Find out how.

Running the Example in a Custom Partial Trust Mode
To enable the data encryption/decryption features, and still allow Enterprise Library features to execute in partial trust, you must create a custom policy definitions file that contains the required permission grants, and then specify that as the trust mode for your application. In fact, the example application requires only one modification to existing permissions and the three extra permission grants shown earlier in this article. The Caching Application Block requires permission to serialize data:

   <IPermission class="SecurityPermission" version="1"
       Flags="Assertion, Execution, ControlThread, ControlPrincipal,
       RemotingConfiguration, SerializationFormatter" 
Meanwhile, the Data Access Application Block, Logging Application Block, and Cryptography Application Block require new permissions added to the custom policy definitions file:

   <!-- custom OLEDB permission -->
   <IPermission class="OleDbPermission" version="1">
     <add ConnectionString="Provider=SQLOLEDB;Database=..."
          KeyRestrictions="" KeyRestrictionBehavior="AllowOnly"/>
   <!-- custom Event Log permission -->
   <IPermission class="EventLogPermission" version="1">
     <Machine name="." access="Administer"/>
   <!-- custom Data Protection permission -->
   <IPermission class="DataProtectionPermission" version="1"
                Flags="ProtectData, UnprotectData" />
Figure 5. Applying Custom Trust Mode: In the custom trust mode, the OLE-DB provider can execute using the specified connection string.
To force the application to run under the new trust mode requires only a change to the <trust> element in the application's Web.config file to specify the CustomMedium policy:

   <trust level="CustomMedium" originUrl="" />
Using the custom trust mode, the application opens without any errors. You can see the data retrieved from the database in the right-hand list. The OLE-DB provider can run using the connection string specified in the policy definitions file (see Figure 5).

Clicking the button to create and cache a DataSet also works fine with the extra permission grant that allows the Caching Application Block to serialize the data before writing it to Isolated Storage. You can see in Figure 5 that the page indicates one item was added to the Caching Application Block cache. Click the button to load the DataSet back from the cache, and you see the data displayed in a GridView control on the page (see Figure 6).

Figure 6. Retrieving a Cached Dataset: The figures shows the DataSet that was stored in the Caching Application Block cache.
Figure 7. Encryption Works in Custom Trust Mode: In the custom trust mode, the Cryptography Application Block can encrypt data.
Figure 8. Decrypted Data: Here's the decrypted data that was stored in the user's session with the Cryptography Application Block.
The custom trust mode and the corresponding permissions specified by the CustomMedium policy definitions file also allow the Cryptography Application Block to encrypt and decrypt data. Clicking the Encrypt button generates the encrypted version of the text in the text box above the buttons, and displays it in the text box below the buttons (see Figure 7).

The encrypted data is stored in the Isolated Storage cache (using the Caching Application Block), and retrieved and decrypted when you click the Decrypt button. You can see how the Cryptography Application Block returns the original text, and displays in the lower text box (see Figure 8).

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date