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

Source: Internet
Author: User
Tags connectionstrings
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 that the encryption method is-VF, And the decryption method is-PDF. Do not make a mistake.

Link: http://www.quxiang.net/blog/article/Program/137.htm

The following is a supplement:
If you access the web program, the page prompts error message from the provider: The RSA key container cocould not be opened.
This is because the network service account cannot access the key file. Find the key file and grant the network service read permission. The key file is located (the key file generated by you can be found in chronological order)
Vista: C: \ programdata \ Microsoft \ crypto \ RSA \ machinekeys \
XP or other: C: \ Documents ents and Settings \ All Users \ Application Data \ Microsoft \ crypto \ RSA \ machinekeys

If not, run the following command in cmd:
Aspnet_regiis-pa "netframeworkconfigurationkey" "nt authority \ Network Service"

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.