ASP. NET encrypts viewstate.
Page. registerrequiresviewstateencryption
The method is to register the control as the control that requires view State encryption. If you want to develop a custom control for processing potential sensitive information, call the registerrequiresviewstateencryption method to register the control with the page and ensure that the view status information of the control is encrypted.
The registerrequiresviewstateencryption method must be
Prerender Called in or before this phase. The following code is rewritten in the viewstatecontrol control.
Onprerender And add the view encryption function.
Code Section:
Protected override void onprerender (eventargs E)
{
This. Page. registerrequiresviewstateencryption ();
Base. onprerender (E );
}
Compile the control and run it again in the browser. You can see that the value of the hidden Domain View Control sent to the client is:
It becomes an encrypted hash code:
You can also specify the encryption mode of view status information, which is set by system enumeration viewstateencryptionmode. This enumeration can control whether to encrypt view status information. It has three enumeration items.
Enumeration member description
Auto: if the control requests Encryption by calling the registerrequiresviewstateencryption method, the view status information is encrypted; otherwise, the view status is not encrypted. This value is the default value of the page. viewstateencryptionmode attribute.
Always forcibly encrypts view status information, whether or not to call
Never does not encrypt view status even if the control requests encryption.
In addition to settings in the control, you can also configure the application in the configuration file.
Program Level, or configure the page level in the page. When using a widget or page, make the following settings:
This. Page. viewstateencryptionmode = viewstateencryptionmode. Auto;
This. Page. registerrequiresviewstateencryption ();
after the preceding settings, the page framework automatically performs encryption and decryption. Add two more points to this function:
1. The encrypted view State will definitely affect the application performance. Therefore, exercise caution when using this function.
2. As long as any widget on the page needs to encrypt the view state, all view states on the page will also be encrypted. Currently, Asp. net has not implemented the encryption function of a control view on the needle page (the method of the page control page is called, whether in the control or on the page, registerrequiresviewstateencryption, and this method does not have any reload methods ).