Solved

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

Posted on 2011-03-12
5
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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