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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get virtual path of web.config from global.aspx

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
can someone help me how to get to the web.config file? I want to get to that to encrypt the connection string. I get blank path when I run the code below....Thank you for the help.

Code:
 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs on application startup   
           
        ' Get the file path   
        Dim path As String = HttpContext.Current.Request.CurrentExecutionFilePath   
        path = path.Substring(0, path.LastIndexOf("/"))   
  
        ' Get the appSetting and connectionStrings sections   
        Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(path)
        Dim appSettings As ConfigurationSection = config.GetSection("appSettings")
 
Thank you Mark for the reply. Actually, I got the code from your website.I checked the file path and instead of accessing the web.config file form the server it is going to another place. I just copy the code as it is to the global.asax file. thanks

here is the code and path.
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\web.config

Code:
<%@ Application Language="VB" %>   
<%@ Import Namespace="System.Configuration" %>   
<%@ Import Namespace="System.Web.Configuration" %>   
  
<script runat="server">   
  
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs on application startup   
           
        ' Get the file path   
        Dim path As String = HttpContext.Current.Request.CurrentExecutionFilePath   
        path = path.Substring(0, path.LastIndexOf("/"))   
  
        ' Get the appSetting and connectionStrings sections   
        Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(path)   
        Dim appSettings As ConfigurationSection = config.GetSection("appSettings")   
        Dim connectionSettings As ConfigurationSection = config.GetSection("connectionStrings")   
           
        ' Encrypt the appSettings and connectionStrings sections if they are not already protected   
        If appSettings.SectionInformation.IsProtected = False Then  
            appSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")   
            ' To unprotect this section, use:   
            'appSettings.SectionInformation.UnprotectSection()   
        End If  
        If connectionSettings.SectionInformation.IsProtected = False Then  
            connectionSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider")   
            ' To unprotect this section, use:   
            'connectionSettings.SectionInformation.UnprotectSection()   
        End If  
           
        Try  
            config.Save()   
        Catch ex As Exception   
            ' If an error occurs, it is most likely a permissions error   
            ' so make sure the ASP.NET process account has write permissions for the web.config file   
        End Try  
    End Sub  
       
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs on application shutdown   
    End Sub  
           
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs when an unhandled error occurs   
    End Sub  
  
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs when a new session is started   
    End Sub  
  
    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)   
        ' Code that runs when a session ends.    
        ' Note: The Session_End event is raised only when the sessionstate mode   
        ' is set to InProc in the Web.config file. If session mode is set to StateServer    
        ' or SQLServer, the event is not raised.   
    End Sub  
          
</script>
 
Code:
config.FilePath :
value:   c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\web.config

HttpContext.Current.Request.CurrentExecutionFilePath: 

value:   /ItemDetail.aspx
 
now I change one line of code like this and i am getting a different error message.

Code:
  Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("/")

Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Failed to decrypt using provider 'DataProtectionConfigurationProvider'. Error message from the provider: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B)

Source Error:


Line 11:
Line 12: <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
Line 13: <EncryptedData>
Line 14: <CipherData>


 
Ok now I am able to encrypt the connection string in the web.config file. when I run the page from my local machine everything is working with out any error message.However, when I run it from the dev. environment I get an error message. I am mapping to the development box.

Here is the error.

Parser Error Message: Failed to decrypt using provider 'DataProtectionConfigurationProvider'. Error message from the provider: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B)

Source Error:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top