Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt password in App.config

I want to encrypt the password in connection string. When I make a connection to DB the connection string is openly stored in App.config and I need to find a way to keep only password encrypted.

like image 585
NDeveloper Avatar asked Apr 02 '11 11:04

NDeveloper


People also ask

How do I encrypt a section of web config?

Encrypting a Web Configuration Section To encrypt configuration file contents, use the Aspnet_regiis.exe tool with the –pe option and the name of the configuration element to be encrypted. Use the –app option to identify the application for which the Web.

How do you secure your connection string information?

The best way to secure the database connection string is to encrypt the value within the configuration file. The application would then load the encrypted value from the config file, decrypt the value, and then use the decrypted value as the connection string to connect to the database.


Video Answer


1 Answers

Lets say this is your connection string:

<connectionStrings>     <add name="cs" connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=XXSDFASFDKSFJDKLJFDWERIODFSDFHSDJHKJNFJKSD;"/> </connectionStrings> 

Then you can do something like this:

string myCs = System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString;  System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(myCs); csb.Password = EncDecHelper.Decrypt(csb.Password); myCs = csb.ToString(); 

You can write EncDecHelper.Decrypt by using samples from here: Encrypt and decrypt a string

like image 173
HABJAN Avatar answered Sep 20 '22 13:09

HABJAN