Encrypt web.config Section

ASP.NET 2.o allows you to encrypt sections of web.config file. For example you can have some sensitive information in <appSettings> or some database connection strings in <connectionStrings> and ASP.NET allows you to encrypt those sections and the beauty of this approach is you don’t have to write any decryption code in the application. ASP.NET does it for you automatically.

Using aspnet_regiis utility you can you can encrypt sections of configuration file. Use the following syntax

aspnet_regiis -pef  “appSettings” “c:\inetpub\wwwroot\website1”

The encrypted file might look like the following
I initially tested this code on Dev box (Win XP, VS 2008) it worked without any issue. But when I tried the same approach in Windows Server 2003 I encountered the following error.

Failed to decrypt using provider ‘RsaProtectedConfigurationProvider’. Error message from the provider: The RSA key container could not be opened”

To troubleshoot this issue you need to add the Identity account under which the ASP.NET runs in to the “Key Container”. You might asking what the heck is this Key Container. When you encrypt the sections of web.config file some of the encryption info is stored in this file. (C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA).

To avoid the RSA key error you need to add the Identity info into “Key Container” so this account has access to read the “Key Container”.

This can be done using following command

aspnet_regiis -pa “NetframeworkConfigurationKey” “domain\username”

After executing this command you should not get the error mentioned above.

Other useful resource related to this article


Is WPF Alive or Dead ?

I was really disappointed to read few articles related to “Is WPF Dead?”

I recently started spending so much time on WPF and also passed an MCTS  Exam; and then I came across this article This guy seems to a former Product manager in WPF team and he clearly explains why WPF may be dead…

With such a steep learning curve i’m really not sure whether to continue learning WPF or wait for some definitive answers about the future of WPF…

Even in the recent Tech Ed 2011 in India there is NO event/presentation related to WPF which proves a point to some extent that there’s not much happening in WPF space…




Using C# Events (for beginners)

There are plenty of books on C# and thousands of topics on C# events/delegates. But nothing can beat Head First C# book by O’Reilly. Its so Fun reading the chapters and they way they explained it. I read the chapter about events and delegates and immediately created a small sample application showing the usage of events.

In this example I’ll build a simple stock quote provider and applications that subscribe to this provider to get stock quote.

The first step is to build the stock quote provider class. The StockQuoteProvider class will have an event “UpdateQuote” and a method “OnUpdateQuote” which will raise the event “UpdateQuote”. The “OnUpdateQuote” method will have a parameter “StockQuoteArgs” parameter. The purpose of “StockQuoteArgs” event argment is to send any arguments to the receiver. Basically we will be sending the Stock Code and the current price of the stock to the receiver.







Create a subscriber method as shown below.








Implement a simple windows application to test the StockQuoteProvider and the Subscribers.






Now run the application to test the Stock Quote Provider application

Sample App





Download the source code from here!117&authkey=tobZQeSUrkU%24