Solved

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

Posted on 2011-03-12
5
238 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
ID: 35121417
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
ID: 35121459
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
ID: 35125882
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
ID: 35125931
sorry, my problem. Seem to be specifying wrong connection string some where. Checking and coming back...
0
 

Author Closing Comment

by:Joar
ID: 35126008
Thanks a lot!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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

13 Experts available now in Live!

Get 1:1 Help Now