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

encryption connection string 1

Status
Not open for further replies.

josie2007

Technical User
Apr 14, 2007
90
US
I have my folder, contractors, in the server which is located on c:\contractors and I got the code to encrypt my connection string from I just did like the author of the code has suggested and put the code in the server where I have my other aspx pages. This new global.asax page is located in c:\contractors folders. And when I run my application it was supposed to encrypt the connection string but this is not happening. I am not sure what I did wrong …can someone help me how I can fix my problem. Thanks

here is the 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>
 
Is the global.asax file in the same folder as the web.config file? Also, can you provide the contents of your web.config file?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thank you for your reply. Yes, global.asax and web.config files are in the same folder c:\contractos

here is my web.config file
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration xmlns="

<connectionStrings>
<add name="connContract" connectionString="Data Source=xxxx;Persist Security Info=True;User ID=xxxx;Password=xxxx;Unicode=True"
providerName="System.Data.OracleClient" />
</connectionStrings>

<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="false">
<assemblies>
<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies>
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<customErrors mode="RemoteOnly" defaultRedirect="~/error.aspx">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
</system.web>
</configuration>
 
OK, have you tried this on your development machine? Does it work there? Do you get any errors? Have you stepped through it to see what some of the variables (e.g. "path") contain?


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thanks again for the reply.I have only one machine, production and I have mapped to it.as you suggested I stepped through the code and the variable for the path contains nothing..any clue why
 
Dim path As String = HttpContext.Current.Request.CurrentExecutionFilePath

there is nothing in the path variable once I step through the code.
path=
 
Strange, I can't replicate that problem at all.

As a workaround, you could hard code the path (or place it in the web.config file as a setting). It needs to be set as the virtual path of the site so depending on your setup, I imagine it will be something like:
Code:
Dim path As String = "/contractors"


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thank you for the help.
strange enough when I put this line in the code it works fine.
Dim path As String = "/contractors"
my question is what if I have one dev. server and production server ? can I just go ahead and copy my global.asax code to both servers? will this work without any problem.
 
If you have two servers, you can use the Publish Website option in Visual Studio to publish the site to the live server. It will transfer all the necessary files you need.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top