Tuesday, October 18, 2005

Connection string in ASP.NET 2.0 application

It is a good idea to put connection string into connectionStrings section of Web.Config:
<add name="MyConnectionString" connectionString="Integrated Security=SSPI;Initial Catalog=pubs;Data Source=(local);"/>

Then you can easily read it from your application:
string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

If you want to encrypt your connectionStrings section:
1) Copy Web.Config file into C:\Inetpub\wwwroot\MyWebSite folder.
2) Execute the following command:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pe "connectionStrings" -app "/MyWebSite" -prov "DataProtectionConfigurationProvider"

If command from #2 was successfully executed, your ConnectionStrings section in C:\Inetpub\wwwroot\MyWebSite\Web.Config file is encrypted.
You still can use the same command in order to access your connection string:

VS.NET 2005, C#, ASP.NET 2.0

No comments:


About Me

My photo
Email me: blog@postjobfree.com