The following is an encrypted program, but after the Web. config is uploaded to the server after the local encryption, an error message indicating that the provider "rsaprotectedconfigurationprovider" is not used for decryption will appear. In fact, the solution is very simple. You only need to upload the encrypted program to the server. Run the command. This is easy to use (however, before running the encryption program, Web. config should be unencrypted ). This encryption program code comes from ASP. net2.0 development details of the temple priest
1. Encrypt connectionstrings
Configuration myconfiguration = NULL;
Configurationsection myconnectionstrings = NULL;
Protected void page_load (Object sender, eventargs E)
{
}
// Encrypted with dpapiprotectedconfigurationprovider
Protected void btndpaip_click (Object sender, eventargs E)
{
Try
{
Getconnectionsettings (Out myconfiguration, out myconnectionstrings );
If (! Myconnectionstrings. sectioninformation. isprotected)
{
// Dpapi Encryption
Myconnectionstrings. sectioninformation. protectsection ("dataprotectionconfigurationprovider ");
Myconfiguration. Save (); // The storage settings are written to the Web. config file.
New alertmessage (). showmsg (this. Page, "encrypted with dpaip! ");
}
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}
// Encrypted with rsaprotectedconfigurationprovider
Protected void btnrsa_click (Object sender, eventargs E)
{
Try
{
Getconnectionsettings (Out myconfiguration, out myconnectionstrings );
If (! Myconnectionstrings. sectioninformation. isprotected)
{
// RSA Encryption
Myconnectionstrings. sectioninformation. protectsection ("rsaprotectedconfigurationprovider ");
Myconfiguration. Save (); // The storage settings are written to the Web. config file.
New alertmessage (). showmsg (this. Page, "encrypted with RSA! ");
}
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}
// Obtain the connectionstrings setting block in Web. config.
Protected void getconnectionsettings (out configuration myconfig, out configurationsection connstrings)
{
// Open the Web. config file of the website where the request is located
Myconfig = webconfigurationmanager. openwebconfiguration (request. applicationpath );
// Obtain the connectionstrings setting block in Web. config
Connstrings = myconfig. getsection ("connectionstrings ");
}
// Decrypt
Protected void btndecrypt_click (Object sender, eventargs E)
{
Try
{
Getconnectionsettings (Out myconfiguration, out myconnectionstrings );
Myconnectionstrings. sectioninformation. unprotectsection (); // decrypt
Myconfiguration. Save ();
New alertmessage (). showmsg (this. Page, "connectionstrings decrypted successfully! ");
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}
2. encrypt the appsettings
Configuration myconfiguration = NULL;
Configurationsection mypolicettings = NULL;
Protected void page_load (Object sender, eventargs E)
{
}
// Encrypted with dpapiprotectedconfigurationprovider
Protected void btndpaip_click (Object sender, eventargs E)
{
Try
{
Getappsettings (Out myconfiguration, out myappsettings );
If (! Myettings. sectioninformation. isprotected)
{
// Dpapi Encryption
Mydeleetction. sectioninformation. protectsection ("dataprotectionconfigurationprovider ");
Myconfiguration. Save (); // The storage settings are written to the Web. config file.
New alertmessage (). showmsg (this. Page, "encrypted with dpaip! ");
}
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}
// Encrypted with rsaprotectedconfigurationprovider
Protected void btnrsa_click (Object sender, eventargs E)
{
Try
{
Getappsettings (Out myconfiguration, out myappsettings );
If (! Myettings. sectioninformation. isprotected)
{
// RSA Encryption
Mydeleetask. sectioninformation. protectsection ("rsaprotectedconfigurationprovider ");
Myconfiguration. Save (); // The storage settings are written to the Web. config file.
New alertmessage (). showmsg (this. Page, "encrypted with RSA! ");
}
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}
// Obtain the appsettings setting block in Web. config.
Protected void getappsettings (out configuration myconfig, out configurationsection policettings)
{
// Enable the Web. config file of the website where the request is located
Myconfig = webconfigurationmanager. openwebconfiguration (request. applicationpath );
// Obtain the appsettings setting block in Web. config.
Appsettings = myconfig. getsection ("appsettings ");
}
// Decrypt
Protected void btndecrypt_click (Object sender, eventargs E)
{
Try
{
Getappsettings (Out myconfiguration, out myappsettings );
If (mydeleetask. sectioninformation. isprotected)
{
Myettings. sectioninformation. unprotectsection (); // decrypt
Myconfiguration. Save ();
}
New alertmessage (). showmsg (this. Page, "etettings is successfully decrypted! ");
}
Catch (exception ex)
{
New alertmessage (). showmsg (this. Page, Ex. Message. tostring ());
}
}