Encrypt or Decrypt Web.Config in vb.net ( we can encrypt or decrypt the "connectionStrings" & "appsettings" )

Ranjeet_8
Posted by Ranjeet_8 under VB.NET category on | Points: 40 | Views : 1662
encrypt or decrypt the "connectionStrings" & "appsettings"
Add this on .ASPX page (VB.Net)

<asp:Button ID="btnEncrypt" runat="server" Text="Encrypt WebConfig" />
<asp:Button ID="btnDecrypt" runat="server" Text="Decrypt WebConfig" />
<br />
<br />
<asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>

Write this code on .Aspx.Vb Page

Imports System.Configuration
Imports System.Web.Configuration
Protected Sub btnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
Encrypt("connectionStrings", "DataProtectionConfigurationProvider")
Encrypt("appSettings", "RSAProtectedConfigurationProvider")
lblmsg.Text = "Encrypted... Please close the window."
End Sub
Protected Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
Decrypt("connectionStrings")
Decrypt("appSettings")
lblmsg.Text = "Decrypted... Please close the window."
End Sub
Private Sub Encrypt(SectionName As String, provider As String)
Dim Webconfig As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
Dim Esection As ConfigurationSection = Webconfig.GetSection(SectionName)
If Esection IsNot Nothing AndAlso Not Esection.SectionInformation.IsProtected Then
Esection.SectionInformation.ProtectSection(provider)
Webconfig.Save()
End If
End Sub
Private Sub Decrypt(SectionName As String)
Dim Webconfig As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
Dim Esection As ConfigurationSection = Webconfig.GetSection(SectionName)
If Esection IsNot Nothing AndAlso Esection.SectionInformation.IsProtected Then
Esection.SectionInformation.UnprotectSection()
Webconfig.Save()
End If
End Sub

Comments or Responses

Login to post response