Link to home
Start Free TrialLog in
Avatar of run2004
run2004

asked on

Please help in encrypting the connection string in the Web.config file using ASP.Net 2.0

Hi,

I have tried a couple of things to encrypt and store the database connection string in Web.config file. Please provide me a working example.

In addition to this if I create a custom sections then what will be the beyst way to encrypt the information in that custom section?

Appreciate your help.

~Run2004
Avatar of hfpon
hfpon
Flag of Malaysia image

I am using the Sub below to encrypt my connectionstring for my webconfig
You can modify to add in more webconfig sections.
The usage is just EncryptConfig(True) to encrypt / EncryptConfig(False) turn back to clear text


    Protected Sub EncryptConfig(ByVal bEncrypt As Boolean)
        Dim path = "YourWebConfigPath"
       
        Dim config As 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
Avatar of run2004
run2004

ASKER

The above code is in VB.Net. I am looking for the code in C#. Can you please let me know if I need to decrypt the string before making the calls to the database?

I learnt that we need not to decrypt the string. It's automatically decrypted when ASP.Net makes a call to this. Can you conpfirm?
ASKER CERTIFIED SOLUTION
Avatar of hfpon
hfpon
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial