Solved

How to encrypt and use encrypted connection strings in web.config

Posted on 2011-03-12
5
237 Views
Last Modified: 2012-05-11
In my app I am reading the web.config file to read my connection string by a call to GetWebConfig every time i need access the database
If the connection string is not encrypted it is saved encrypted OK (first time)
Next time I call the function, I get an error on the last statement in the funcion returning the connection string, saying "NullReferenceException was unhandled by user code"
What am I thinking / doing wrong??

   Dim con As SqlConnection
   con = GetWebConfigCon("conPBIB")

------
   Function GetWebConfigCon(ByVal strCon As String) As SqlConnection
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~")
        Dim configSection As ConfigurationSection = config.GetSection("connectionStrings")

       'If not encrypted, do that:        
       If Not configSection.SectionInformation.IsProtected Then
            configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")
            config.Save()
       End If

       'Decrypt (to be able to read? Not save…)
        configSection.SectionInformation.UnprotectSection()

       'Return Connectionstring:
       GetWebConfigCon = New SqlConnection(ConfigurationManager.ConnectionStrings(strCon).ConnectionString)

    End Function

0
Comment
Question by:Joar
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
Comment Utility
For a web.config file with encrypted contents you dont need to Decrypt the  contents  before accessing the connection string. Whenever you access a content of web.config file the decryption is performed automatically.
0
 
LVL 9

Expert Comment

by:mayank_joshi
Comment Utility
Keep in mind that once the data is encrypted, when it's read from an ASP.NET page (i.e., reading the connection string information from a SqlDataSource control or programmatically, via ConfigurationManager.ConnectionStrings[connStringName].ConnectionString), ASP.NET automatically decrypts the connection string and returns the plain-text value. In other words, you don't need to change your code one iota after implementing encryption. Pretty cool!

Reference:-
http://www.4guysfromrolla.com/articles/021506-1.aspx
0
 

Author Comment

by:Joar
Comment Utility
When I remove the statement:
        configSection.SectionInformation.UnprotectSection()

I next time get an error on the last statement in the funcion returning the connection string:
   'Return Connectionstring:
       GetWebConfigCon = New SqlConnection(ConfigurationManager.ConnectionStrings(strCon).ConnectionString)


saying "NullReferenceException was unhandled by user code"
as if it is not finding the value of strCon which is "conPBIB", because it is encrypted?

0
 

Author Comment

by:Joar
Comment Utility
sorry, my problem. Seem to be specifying wrong connection string some where. Checking and coming back...
0
 

Author Closing Comment

by:Joar
Comment Utility
Thanks a lot!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now