Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrypt Connection Strings IIS7

Status
Not open for further replies.

lisat76

Programmer
Sep 25, 2007
89
US
I set up my website to have the ability to encrypt connection strings.
In IIS 6,
I could use the below code to encrypt them.
Now in IIS 7 I get this error

InvalidOperationException
Event handlers can only be bound to HttpApplication events during IHttpModule initialization.

I believe this has to do with IIS seven working a little differently. Not sure what I need to change in my code



Here's my code thanks

Code:
Imports System.Web.Configuration
Partial Class encryptconnect
    Inherits System.Web.UI.Page


    Protected Sub btnEncrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
        Dim Config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        Dim appSettings As ConfigurationSection = Config.ConnectionStrings
        If Not appSettings.SectionInformation.IsProtected Then
            appSettings.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider")
            appSettings.SectionInformation.ForceSave = True
            Config.Save(ConfigurationSaveMode.Modified)
            lblStatus.Text = "Connection Strings were changed and are now Currently Encrypted"
        End If
    End Sub

    Protected Sub btnDecrypt_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
        Dim Config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        Dim appSettings As ConfigurationSection = Config.ConnectionStrings
        If appSettings.SectionInformation.IsProtected Then
            appSettings.SectionInformation.UnprotectSection()
            appSettings.SectionInformation.ForceSave = True
            Config.Save(ConfigurationSaveMode.Modified)
            lblStatus.Text = "Connection Strings were Decrypted and are NOT Encrypted"
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Config As Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        Dim appSettings As ConfigurationSection = Config.ConnectionStrings
        If appSettings.SectionInformation.IsProtected Then
            lblStatus.Text = "Connection Strings are Currently Encrypted"
        Else
            lblStatus.Text = "Connection Strings are NOT Encrypted"
        End If
    End Sub
End Class
 
Actually my code does not thrown an error, If I run it in visual studio. As I said this works fine if I use it under IIS 6. The error I listed comes from windows event log.
The only error I get from the web page itself when running it under IIS is unknown error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top