Precautions for using Asp.net to bring the provider to Web. config for encryption

Source: Internet
Author: User
Tags connectionstrings

 

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 ());
}
}

 

 

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.