Encrypt DB Connection String

Problem:
The web.config file of the website contains the user name and password to the SQL Server database. When uploading a website to another server, whoever has access to the server can read the login info for the database server. We can encrypt this connection string info in the web.config file.

Solution:
Create a page with two buttons. One to encrypt and one to decrypt.

Protected Sub Button_Encrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
EncryptConfig(True)
End Sub

Protected Sub Button_Decrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs)
EncryptConfig(False)
End Sub

Protected Sub EncryptConfig(ByVal bEncrypt As Boolean)
Dim path = “/SCRP”

Dim config As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(path)
Dim appSettings As ConfigurationSection = config.GetSection(“connectionStrings”)

If bEncrypt Then
appSettings.SectionInformation.ProtectSection(“DataProtectionConfigurationProvider”)
Else
appSettings.SectionInformation.UnprotectSection()
End If

config.Save()
End Sub

Other thoughts:
Maybe it’s better to compiled the whole website into .dll files…

Source:
http://www.asp.net/learn/videos/video-39.aspx

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: