Read/write and encryption/decryption of connection strings in web. config

Source: Internet
Author: User

1. let's take a look at how the web. write the database connection string in config. open web. config file. by default, the config file contains two configuration sections: <connectionStrings/> "," <etettings/> ". You can write connection strings in both configuration sections, generally, it is written in <connectionStrings>, for example:
<ConnectionStrings>
<Add name = "connectionString" connectionString = "server = localhost; database = hh_li; User ID = sa; password ="/>
</ConnectionStrings>
Or write it in <deleetask>:
<AppStrings>
<Add key = ''connectionstring" value = "server = localhost; database = hh_li; User ID = sa; password ="/>
</AppStrings>
In the preceding two methods, the value of name and key is the "name" of the connection string. Just like the variable name of a variable, the value of connectionString is the content of the specific connection string.
2. Read the web. config database connection string in asp. net2.0.
Method 1:
String myConn = System. Configuration. ConfigurationManager. ConnectionStrings ["sqlConnectionString"]. ConnectionString;
Method 2:
String connString = System. Web. Configuration. WebConfigurationManager. ConnectionStrings ["sqlConnectionString"]. ToString ();
3. encryption and decryption of connection strings in web. config
Encryption:
The connection strings in the web. config file in asp.net are saved in the form of clear codes. The storage content includes the database server name, database name, account, and password.
To solve this problem, ASP. NET 2.0 supports encryption and storage of sensitive information such as connection strings as Web. methods In the config file. enter. net sdkcommand prompt, use the command line tool aspnet_regiis.exe. the application is located at "% systemroot % \ Micrsoft.. NET \ Framework \ versionNumber "folder. Aspnet_regiis.exe is a common command line interpreter. developers can encrypt or decrypt connection strings by entering commands according to simple syntax requirements.
Assume that the Web. config file to be encrypted is in the "D: \ website2" folder, the command line is as follows.
Aspnet_regiis.exe-Arg "connectionStrings" "D: \ website2"
The parameter "-Ave" indicates that the encryption configuration section is executed based on the absolute path of the file; "connectionStrins" indicates the name of the encrypted configuration section; "D: \ website2" indicates the Web. the absolute path of the config file folder. After the encryption is completed, aspnet_regiis.exe prompts the developer that the encryption is successful.
Decryption: after the connection string is encrypted, it is difficult for others to understand the specific connection string without decryption.
The command for decryption is as follows: aspnet_regiis.exe-pdf "connectionStrings" "D: \ website2" note that a local key is used during encryption, this 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. Note: The encryption method is-Wordpress, And the decryption method is-pdf. Do not make a mistake.

The author's Bai du Id has disabled's blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.