Microsoft Application Block - Enterprise library 4.1 & VS 2008 encrypting/decrypting connectionstring in web.config RRS feed

  • Question

  • User349182294 posted

    I am using Enterprise library Application Block to access the database as like below. My database connection is in web.config. I would like to encrypt the connection string. Could any one help me to encrypting/decrypting  the connection string. 

    	StringBuilder sQuery = new StringBuilder();
                    sQuery.Append(" SELECT COUNT(*) FROM ADMIN ");
                    sQuery.Append(" WHERE INACTIVE_FLAG = 'N' ");
                    sQuery.Append(" AND USER_ID =:USER_ID");
                    DbCommand command = db.GetSqlStringCommand(sQuery.ToString());
                    db.AddInParameter(command, "USER_ID", DbType.String, "nimonu");
                    bool b = Convert.ToBoolean(db.ExecuteScalar(command));
                catch (Exception ex)
                    throw ex;

    I was trying to encrypt the connection string using  the below command in the command prompt

    aspnet_regiis.exe -pe ConnectionString -app "/TeamTime"  (is virtual directory). This code encrypt the connectionstring, but the above C# code not reading the encrypted connection string.

    Appreciate your time and response.

    Thursday, January 6, 2011 7:17 PM


All replies

  • User1836575615 posted


    check out this article: http://www.codedigest.com/CodeDigest/107-Encrypting-Decrypting-Connection-String-in-ASP-Net.aspx

    you have the option of using the RSAProtectedConfigurationProvider or the DataProtectionConfigurationProvider to secure the connection string.

    If you would like to know more about DPAPI check out the MSDN article at http://msdn.microsoft.com/en-us/library/ms995355

    Friday, January 7, 2011 11:08 AM
  • User-2139489267 posted

    I think you're missing

    Database db= DatabaseFactory.CreateDatabase();
    Tuesday, January 11, 2011 1:00 AM
  • User349182294 posted

    Thanks for the reply. I have used the below web page code to encrypt/ decrypt the connectionstring. 


    It is working good in my system. When i am trying to deploy to QA/PROD environment, It seems needs to give full read and write access to the virtual directory to encrypt the connectionstring. I gave access its encrypted and then I revoke the access. It works fine now.

    I can do it in QA environment. But I could not do these in the Prod environment. Is there any other way to encrypt the connectionstring without giving the read and write access to the virtual directory.

    PS: I have copied the web.config from my development server to the QA It was not working.

    Wednesday, January 12, 2011 1:49 PM
  • User349182294 posted

    Sorry, I just found an useful link from Microsoft site


    I am not doing the encryption using C# code. I Am doing it from the command utility as like below. 

    I am using the "DataProtectionConfigurationProvider" 

    To Encrypt :

    aspnet_regiis.exe -pe "connectionStrings" -app "/HFM UILayer" -prov "DataProtectionConfigurationProvider"

    To Decrypt:

    aspnet_regiis.exe -pd "connectionStrings" -app "/HFM UILayer" 

    Wednesday, January 12, 2011 8:24 PM
  • User-2139489267 posted

    I am not doing the encryption using C# code. I Am doing it from the command utility as like below. 

    Is it working now? Any problem?

    Please check out the following link:


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 16, 2011 11:24 PM