Mist
Mist

Reputation: 684

ASP.Net MVC5:How to encrypt decrypt connection string in web.config

Just read this article http://tutorialslink.com/Articles/Encrypt-and-Decrypt-Connection-Strings-in-Webconfig/52

they said if we use this command then connection string will be encrypted aspnet_regiis.exe -pef "connectionStrings" "<Path of the Folder containing the Web.Config file>" and say this command will decrypt the connection string aspnet_regiis.exe -pdf "connectionStrings" "<Path of the Folder containing the Web.Config file>"

but the problem is if we follow this approach it may not work when we host our web site in different pc.

so please tell me what approach we should follow to encrypt / decrypt connection string or any section in web.config which will work in any pc.

thanks in advance.

Upvotes: 1

Views: 2910

Answers (1)

Baksteen
Baksteen

Reputation: 1325

I don't know how sensitive your data is, but I suppose you could try the following:

  1. Encrypt the connectionString manually first (for instance, with AES(Rijndael)).
  2. Paste the encrypted string in your web.config.
  3. Decrypt the string in your code, using something like this

    private string getConnectionString()
    {
        string encrypted = System.Configuration.
              ConfigurationManager.AppSettings["connectionString"];
    
        //Rijndael or any other form of decryption here...
        //.....
        //.....
    
        return decryptedString;
    }
    
  4. Use the decrypted connectionString to connect to your database! :)

Upvotes: 1

Related Questions