GeekInterview.com
Series: Subject: Topic:
Question: 15 of 348

Encrypt connection string in Web.config

How to Encryption and Decryption connection string in Web.config ? Explain with Example ?
Asked by: rajankvns | Member Since Sep-2011 | Asked on: Sep 18th, 2011

View all questions by rajankvns   View all answers by rajankvns

Showing Answers 1 - 2 of 2 Answers
Sudhakar

Answered On : Apr 4th, 2012

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
rupinder

Answered On : 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.  

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.