0

I am using Entity Framework to create a SQLServer connection of database with a password:

connectionString="DataSource=|DataDirectory|DB.sdf;Password=abc

How can I protect the password while using Entity Framework?

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
Alvin
  • 8,219
  • 25
  • 96
  • 177

3 Answers3

1
    ConnectionStringsSection oSection = Configuration.ServiceConfiguration.GetConnectionStrings();
    if(!oSection.SectionInformation.IsLocked && !oSection.SectionInformation.IsProtected)
    {
        oSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); 
        oSection.CurrentConfiguration.Save();
    }
Nick Jones
  • 6,413
  • 2
  • 18
  • 18
  • Does this work with entity framework, because the code above will encrypt the connection string. – Alvin Sep 04 '12 at 12:44
0

If you have access to the server, you can use the web config encryption tool from cmd. I create a .bat file in the same folder as my web.config containing:

aspnet_regiis -pef connectionStrings .
aspnet_regiis -pef appSettings .
pause

This will encrypt the connectionStrings whole section and in my case, the appSettings whole section too.

To Decrypt, do:

aspnet_regiis -pdf connectionStrings .
aspnet_regiis -pdf appSettings .
pause

You will need to run this from the server though.

Rodders
  • 2,425
  • 2
  • 20
  • 34
0

You can write EncDecHelper.Decrypt by using samples from here: Encrypt/Decrypt string in .NET

You will find the related information from here: Encrypt password in App.config

Community
  • 1
  • 1
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105