Question
I am trying to programmaticaly encrypt the connection string in the web config file usingaspnet 3.5
This worked fine with 2.0 but when I do this with 3.5 the connection string is encrypted and the application
runs but I am getting two error messages:
1) I am getting a wavy blue line under "configProtectionProvider" (see below for reference)
and the error message ("configProtectionProvider" attribute not declared)
2) and I am getting a wavy blue line under<EncryptedData>
and the error message (The element 'configurationStrings' has an invalid child element 'EncryptedData')(see below for reference)
Can anyone tell me what I am doing wrong
Many thanks
Martin
....................................................................................
This is the encoded connection string. I have shortened the cypher value for brevity
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAHzCqRHlR9U2JNbAd==</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
......................................................................................
This is the Aspx page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div><asp:Button ID="Button1" runat="server" Text="encrypt" /></div>
</div>
<p></p>
<div>
<div><asp:Button ID="Button2" runat="server" Text="decrypt" /></div>
</div>
</form>
</body>
</html>
......................................................................................
This is the Aspx.vb page
Imports System.Configuration
Imports System.Web.Security
Imports System.Web.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Public Sub EncryptConnStr(ByVal protectionProvider As String)
'---open the web.config file
Dim config As Configuration = _
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
'---indicate the section to protect
Dim section As ConfigurationSection = config.Sections("connectionStrings")
'---specify the protection provider
section.SectionInformation.ProtectSection(protectionProvider)
'---Apple the protection and update
config.Save()
End Sub
Public Sub DecryptConnStr()
Dim config As Configuration = _
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( _
Request.ApplicationPath)
Dim section As ConfigurationSection = _
config.Sections("connectionStrings")
section.SectionInformation.UnprotectSection()
config.Save()
End Sub
Protected Sub EncryptConnStrings_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
'Get configuration information about Web.config
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
' Let's work with the <connectionStrings> section
Dim connectionStrings As ConfigurationSection = _
config.GetSection("connectionStrings")
If connectionStrings IsNot Nothing Then
' Only encrypt the section if it is not already protected
If Not connectionStrings.SectionInformation.IsProtected Then
' Encrypt the <connectionStrings> section using the
'DpapiProtectedConfigurationProvider(Provider)
connectionStrings.SectionInformation.ProtectSection( _
"DataProtectionConfigurationProvider")
config.Save()
' Refresh the Web.config display
End If
End If
End Sub
Protected Sub DecryptConnStrings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
DecryptConnStr()
End Sub
End Class