
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1006
|
Chapter 17: Security
You will need to add the System.Web and System.Configuration DLLs to your project
before this code will compile.
Discussion
To encrypt data, you can call the EncryptWebConfigData method with the following
arguments:
EncryptWebConfigData("/WebApplication1", "appSettings",
"DataProtectionConfigurationProvider");
The first argument is the virtual path to the web application, the second argument is
the section that you want to encrypt, and the last argument is the data protection
provider that you want to use to encrypt the data.
The
EncryptWebConfigData method uses the virtual path passed into it to open the
web.config file. This is done using the
OpenWebConfiguration static method of the
WebConfigurationManager class:
System.Configuration.Configuration webConfig =
WebConfigurationManager.OpenWebConfiguration(appPath);
This method returns a System.Configuration.Configuration object, which you use to
get the section of the web.config file that you wish to encrypt. This is accomplished
through the
GetSection method:
ConfigurationSection webConfigSection = webConfig.GetSection(protectedSection);
This method returns a ConfigurationSection object that you can use to encrypt the
section. This is done through a call to the
ProtectSection method:
webConfigSection.SectionInformation.ProtectSection(dataProtectionProvider); ...