Cannot be able to encrypt the connectionstring in web.config file!

Posted by Akiii under C# on 6/5/2011 | Points: 10 | Views : 2360 | Status : [Member] | Replies : 4
Hi,
I am trying to encrypt the connection string that is stored in web.config file by programming. However, i am not being able to do that. Below is my code:-

using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;

public void EncryptConnString()
{

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
config.Save();
}
}


Please suggest me some measures and if i am wrong correct me...

Thanks and Regards
Akiii




Responses

Posted by: Sksingh on: 6/5/2011 [Member] Starter | Points: 25

Up
0
Down
Suppose below is connection string that you want to encrypt:

<connectionStrings>
<add name="MyConnection"
connectionString="Initial Catalog=aspnetdb;
data source=localhost;Integrated Security=SSPI;"
providerName="System.Data.SqlClient"/>
</connectionStrings>


You can encrypt using visual studio command :
aspnet_regiis -pe "connectionStrings" -app "/TestApp"
where TestApp is virtual directory name is IIS and connectionStrings is config section to encrypt.

If you want to decrypt then use below command :
aspnet_regiis -pd "connectionStrings" -app "/TestApp"

Note : Like above even you can encrypt other config sections also.
Imp: Same can be achieve by code behind also.
Cheers !!!!!!!!!!!!





Regards,
Sunil

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Poster on: 6/5/2011 [Member] Starter | Points: 25
Posted by: Srinath on: 6/5/2011 [Member] Starter | Points: 25

Up
0
Down
Thank u for ur posting.....have a nice day.

Srinath.K

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 6/6/2011 [Member] Bronze | Points: 25

Up
0
Down
@Sunil.....

Thanks for your answer......
i have successfully encrypted the connectionstring in the web.config file.......
and its working absolutely fine.....

Thanks for ur support..

@poster....
The link that u have provided is asking for username and password of the itfunda website.....

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response