This document demonstrates how Trust Services can be used to encrypt sensitive information stored in ASP.NET web.config files.
When the application is up and running, ASP.NET automatically takes care of decrypting configuration setting value when the application tries to access it. To learn more about the encryption/decryption of config files, please go to: http://msdn.microsoft.com/en-us/library/zhhddkxy.aspx. The key here is to configure the appropriate configuration provider in your Web.Config. Let's take a closer look at how Trust Services is used along with the 'Protected Configuration Provider' to perform encryption/decryption. Details
Out of the box, ASP.NET includes two protected config providers that can be used to encrypt sections of a Web.Config file:
This sample provides a custom implementation of a protected configuration provider that uses Trust Services. The custom implementation of the provider is in the project "TfsProtectedConfigurationProvider". It implements the "Encrypt" and "Decrypt" methods that will be invoked during encryption and decryption respectively. It also implements the "Initialize" method that sets property values for the provider instance which are used to create a Trust Services instance and to perform transformations with it.
Required Parameters for the Config Provider:
1. If you choose to use the certificate that is included in the solution folder, you must install it locally (in LocalMachine\Personal store). The password for it is "trust".
Add Policy to encrypt the Config Section: 1. Create a data policy using the powershell script "CreatePolicy.ps1" located in the solution folder.
Configure TfsProtectedConfigurationProvider in Web.Config: 1. Configure the provider in web.config with the following info: a. 'trustServerName' parameter - This should be the trust server you had used in the previous step. b. 'thumbprint' parameter - You can leave this if you had used the 'PolicyAdmin.pfx' certificate, otherwise change it to the appropriate thumbprint.
Encrypt web.config: 1. Once the solution is successfully compiling, open a Visual Studio or Windows SDK command prompt and navigate to the solution folder. - If you don't have either Visual Studio or Windows SDK installed on your machine, you can choose to download the Windows SDK from here: http://msdn.microsoft.com/en-us/windowsserver/bb980924.aspx. 2. Make sure correct values are configured in the EncryptConfig.cmd script. 3. Run EncryptConfig.cmd to encrypt the "connectionStrings" config section in web.config located in the "debug" build folder. 4. Open Web.Config in the path mentioned above, and make sure you see something like this: "...<connectionStrings configProtectionProvider="TfsProtectedConfigurationProvider"> <EncryptedData>....</EncryptedData> </connectionStrings>....". Notice that "connectionStrings" section has been encrypted.
Create Service Package: 1. To include the web.config in the service package, you can do this manually through Visual Studio:
2. To do this through command line, there are a couple of options:
Before deploying to service: 1. Create a hosted service 2. Upload "PolicyAdmin" (password = "trust") and "SampleWebUi-Remote" (password = "test") certificates to Azure Portal.
Enabling remote desktop to verify that the config file is encrypted: 1. The sample ships with a sample certificate that can be used to enable Remote Desktop connection. 2. If you have not made any changes to the Service configuration file, then RDP should have been enabled. The credentials are UserId/Pwd - test/RdcRdc2.
After service is deployed: 1. After remoting to the machine, open "E:\approot\web.config" file to ensure that the config sections are encrypted. 2. Navigate to the site (<dnsname>.cloudapp.net) and on the home page you should see the connection string displayed in clear text. This is just to show that the ASP.NET process was able to successfully decrypt the config section!
Learn More
Getting Started Tutorial
Request Registration Code
Download "Trust Services" SDK
Access "Trust Services" Portal
Samples
Troubleshooting
SQL Azure Labs Forums