[At 2009-5-17 By heimianadmin Add comments]
Encryption and decryption of database connection strings in ASP. NET web. config.
Although not very fresh, I believe there are still many people who don't know, well, don't talk nonsense, just give the method: Start ---> Run, Enter cmd, and then enter the following content
Encryption:
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_regiis.exe-Arg "connectionStrings" "Your Web project path"
Decryption:
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_regiis.exe-pdf "connectionStrings" "Your Web project path"
. NET is the version path, and the name of the connectionStrings connection string is modified by yourself.
Note that a local key is used in the encryption process, which means that the decryption process must be completed on the same computer. If the encrypted Web. config file is moved to another computer, the connection string in the Web. config file cannot be decrypted normally.
Asp.net2.0implements simple encryption, that is, using the aspnet_iis.exe command. The command location is as follows:
C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727
Note: The specific situation varies depending on the system location and version number.
In command mode, enter the directory to run. The complete command is:
Aspnet_iis-VF "connectionStrings" "Web. cofing absolute path (Note: you do not need to enter web. config )"
If it is normal, the message "succeeded" is displayed. When Web. config is enabled, the string is encrypted.
The decryption command is:
Aspnet_iis-pdf "connectionStrings" "Web. cofing absolute path (Note: you do not need to enter web. config )"
Note that encryption and decryption must be completed on one machine.
-----------------------------
The following shows how to encrypt the database connection string through Code. The Code is as follows:
Configuration config = WebConfigurationManager. OpenWebConfiguration (Request. ApplicationPath );
ConfigurationSection configSection = config. GetSection ("connectionStrings ");
If (configSection. SectionInformation. IsProtected)
... {// If it has been encrypted, no further encryption is required.
ConfigSection. SectionInformation. UnprotectSection ();
Config. Save ();
}
Else
...{
ConfigSection. SectionInformation. ProtectSection ("DataProtectionConfigurationProvider ");
Config. Save ();
}