Hello sir, i want to know that how i encrypt the connection string in asp.net.Please help me...
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hemant KumarPosted Sep 21, 2011, 5:34 AM
http://codes.codedigest.com/CodeDigest/107-Encrypting-Decrypting-Connection-String-in-ASP-Net.aspx
Hemant KumarPosted Sep 21, 2011, 5:33 AM
You can take a part of the web.config file and encrypt it so that it can't be read unless the key is used to read it. This helps protect the web.config file and sensitive data that it contains. ASP.Net automatically decrypts the data as needed.
I've written information regarding choosing between the Machine Key or User-Level Key which I moved to the bottom of this article in case of interest.
The aspnet_regiis.exe tool and its syntax
To encrypt the data, you must use a command line utility called aspnet_regiis.exe which exists in the framework folder. There are two options that work for encryption, -pe and -pef. Here is what the help file shows:
-pe section Encrypt the configuration section. Optional arguments:
[-prov provider] Use this provider to encrypt.
[-app virtual-path] Encrypt at this path level.
[-location sub-path] Location sub path.
[-pkm] Encrypt/decrypt the machine.config instead of
web.config.
-pef section web-app-physical-dir
Encrypt the configuration section. Optional arguments:
[-prov provider] Use this provider to encrypt.
The difference is how the tool locates the config file to encrypt.
For example: aspnet_regiis -pe "connectionStrings" -app "w3svc/72/root/" -prov "CustomProvider" (see the channel9 article in the link above for more details about the custom provider and the connectionStrings section)
For example: aspnet_regiis -pef "connectionStrings" "C:\Inetpub\Site72.com" -prov "CustomProvider"
Notice that -pef doesn't have an -app parameter, the path immediately follows the section (the part of web.config that is encrypted).
After noticing these differences today, I brought it up to the ASP.NET team. I haven't relieved a response yet because I just submitted it but I'm hoping that it's early enough to make these more consistent before the final release of v2.0. There is one more important difference that I brought up to the ASP.NET team today. I'll only briefly cover it now since it's not really what I meant to write about. (For further explanation of the terms, read the article that I've referenced above.) The -prov usage is different between the two. With -pe, it's the 'keyContainerName' of the key container that is required but with -pef, it's the 'name' property of the key container that is required. This doesn't come into play unless you use a custom provider.
The key and its storage location
As for the key and where it is stored, obviously it can't be contained in the same site location as the site or if someone gained access to the web.config file, they would gain access to the key as well, thus defeating the purpose. So, the key is stored in \Documents and Settings. There are two levels of key storage
In theory, in a trusted environment where everyone on the same server trusts each other, you can use the machine key so that everyone can share the key. The user-level key is supposed to be used in a shared environment where different applications on the same server don't trust each other.
That's the theory, but what about practical use? Well, that's a loaded question. This key can be used by web applications and windows applications, so of course the usage will vary. There is certainly a use for user-level keys, but in a shared hosted environment, I believe it's actually better to use the machine key and then lock it down with NTFS permissions and a custom anonymous user on the site with impersonation in ASP.NET turned onSatish BhatPosted Sep 21, 2011, 5:01 AM