Mahdi Ghiasi
Mahdi Ghiasi

Reputation: 15301

Encrypt web.config without command line tool

I want to encrypt my appSettings and ConnectionStrings that located on my web.config file.

But since I'm using a shared hosting, I can't use aspnet_regiis.exe method.

Is there any other way to encrypt web.config file? (an online tool, a remote tool, a programming way, or...)

Upvotes: 0

Views: 778

Answers (1)

Waqar Janjua
Waqar Janjua

Reputation: 6123

Not tested by me, from CodeProject, hope that it will work Aspx Code:

 <asp:Button id="btnEncrypt" runat="server" Text="Encrypt" onclick="btnEncrypt_Click" />
 <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" onclick="btnDecrypt_Click" />

 Code Behind:

    string provider = "RSAProtectedConfigurationProvider";
    string section = "connectionStrings";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnEncrypt_Click(object sender, EventArgs e)
    {
       Configuration confg = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
       ConfigurationSection configSect = confg.GetSection(section);
       if (configSect != null)
       {
          configSect.SectionInformation.ProtectSection(provider);
          confg.Save();
       }
    }

    protected void btnDecrypt_Click(object sender, EventArgs e)
    {
       Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
       ConfigurationSection configSect = config.GetSection(section);
       if (configSect.SectionInformation.IsProtected)
       {
          configSect.SectionInformation.UnprotectSection();
          config.Save();
       }
    }

http://www.codeproject.com/Tips/304638/Encrypt-or-Decrypt-Connection-Strings-in-web-confi

Upvotes: 2

Related Questions