Encrypt connection string in Web.config

How to Encryption and Decryption connection string in Web.config ? Explain with Example ?

Questions by rajankvns   answers by rajankvns

Showing Answers 1 - 2 of 2 Answers

Sudhakar

  • Apr 4th, 2012
 

You can use server.EncryptUrl , Server.DecryptUrl Functions.

rupinder

  • Apr 17th, 2013
 


methods for Encrypting and decrypting connectionStrings
section in web.config
you can use encryption configuration model " Rsa,"
it is feasible to change this to DataProtectionConfigurationProvider

Code
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Text;

  4. using System.Web;

  5. //specific to configuration

  6. using System.Configuration;

  7. using System.Web.Configuration;

  8. using System.Web.Security;

  9. namespace WebUtils

  10. {

  11. /// <summary>

  12. /// Contains methods for Encrypting and decrypting connectionStrings

  13. /// section in web.config

  14. /// current encryption configuration model is Rsa,

  15. /// it is feasible to change this to DataProtectionConfigurationProvider

  16. /// </summary>

  17. public class EncryptDecrypt

  18. {

  19. //Get Application path using HttpContext

  20. public static string path = HttpContext.Current.

  21. Request.ApplicationPath;

  22. #region Encrypt method

  23. public static void EncryptConnString()

  24. {

  25. Configuration config = WebConfigurationManager.

  26. OpenWebConfiguration(path);

  27. ConfigurationSection section =

  28. config.GetSection("connectionStrings");

  29. if (!section.SectionInformation.IsProtected)

  30. {

  31. section.SectionInformation.ProtectSection

  32. ("RsaProtectedConfigurationProvider");

  33. config.Save();

  34. }

  35. } #endregi

  36. #region Decrypt method

  37. public static void DecryptConnString()

  38. {

  39. Configuration config = WebConfigurationManager.

  40. OpenWebConfiguration(path);

  41. ConfigurationSection section =

  42. config.GetSection("connectionStrings");

  43. if (section.SectionInformation.IsProtected)

  44. {

  45. section.SectionInformation.UnprotectSection();

  46. config.Save();

  47. }

  48. }

  49. #endregion

  50. }

  51. }

  52.  

  Was this answer useful?  Yes

Rodger Svovah

  • May 25th, 2014
 

You encrypt connection string string by using aspnet_regiis.exe like so:
aspnet_regiis.ex -pe "connection string" -app "MyWebApp"

To decrypt you only need to change the option -pe to -pd.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions