When deploying a project, we always need to encrypt the connectionstrings or appsettings configuration section in the configuration file to prevent the user database from being leaked .. Net FrameworkProgramModel to encrypt the configuration section. The framework contains two protectedconfigurationprovider: rsaprotectedconfigurationprovider and dpapiprotectedconfigurationprovider. the former is the default provider. We try to use this type because it supports importing/exporting encryption keys. In other words, you can move the configuration section from one Web server to another, for example, you can encrypt the configuration section on the machine you developed and deploy the application on the product server.
If the latter is used to encrypt the configuration section, the configuration section cannot be decrypted on another Web server. If you want to move the configuration file between different servers, decrypt the configuration section on the employee server, re-encrypt the configuration file on the target server.
1.use aspnet_regiis.exe to encrypt the configuration section
This is the most convenient method for encrypting a configuration section in the Web configuration file.
1C: \ windows \ Microsoft. NET \ framework \ V2.0.50727
Directory
You can manually enter the path in the cmd dos window to use this tool, or directly find the Microsoft Visual Studio 2005-Visual Studio Tools-Visual Studio 2005 command prompt in the Start Menu. Open the doswindow and enter the path of the aspnet_regiis.exe tool.
Assume that we have a website in D: \ projects \ mywebsite.
We need to encrypt the website's webconfig's appsettings configuration section as follows:
1Aspnet_regiis-em ettings"D: \ projects \ mywebsite"
The above is the absolute path, and the relative path can also be used.
1Aspnet_regiis-em ettings"D: \ projects \ mywebsite"
-App indicates the virtual path of the specified application
Open the Web. config file again and find that the appsetting configuration section is encrypted.
Of course, to encrypt the connectionstrings configuration section, you only need to addCodeReplace the deleetting.
We should note that the-PLD (protect encrypt filepath) option is used to encrypt the specified configuration section of the web. config file in the specified path.
Of course there is decryption ....
We can use the-PDF option to decrypt the configuration section and take the above example
1Aspnet_regiis-PDF appsettings"D: \ projects \ mywebsite"
Virtual Path
1Aspnet_regiis-Pd appsettings-APP/mywebsite
Suppose we use rsaprotectedconfigurationprovider to encrypt the configuration file. Now we need to copy an encrypted configuration file from one server to another, then we must copy the key of the encrypted configuration file to the new server, otherwise, the application cannot read the encrypted configuration file on the new server.
By default, the container that stores the key is in the following folder:
1\ Document and Settings \ All Users \ Application Data \ Microsoft \ crypto \ RSA \ machinekeys
Of course, you may not be able to find this folder (O (∩ _ ∩) O). Now, for example, tools such as 360 file migration, the ghost knows, will those folders be moved somewhere else .... find out more
Mine is in
1C: \ programdata \ Microsoft \ crypto \ RSA \ machinekeys
The machinekeys directory contains system files consisting of numbers and letters. This is what we are looking.
We must also configure a new key container to import the key container to the new server. The following steps must be completed in sequence:
1-create a new key container
1Aspnet_regiis-PC"Sharedkeys"-Exp
Sharedkeys is the new key container.-exp indicates that the secret of the container can be exported.
2-configure the application to use the new key container
Web. config:
1 < Configuration > 2 < Configprotectdata Defaultprovider = "Myprotectedconfigurationprovider" > 3 < Providers > 4 < Add Name = "Myprotectedconfigurationprovider" Type = "System. configuration. rsaprotectedconfigurationprovider" Cspprovidername = "" Usemachinecontainer = "True" Useoaep = "False" Keycontainername = "Sharedkeys" /> 5 </ Providers > 6 </ Configprotecteddata > 7 </ Configuration >
3-export the key in the sharedkeys container to a keys. xml file.
1Aspnet_regiis-Px"Sharedkeys"Keys. XML-pri
4-copy the created keys. xml file to the target server and import the key
1Aspnet_regiis-pi"Sharedkeys"Keys. xml
5-grant the Asp.net application the permission to access the key container
1Aspnet_regiis-pa"Sharedkeys" "ASPnet"