This section describes the encryption capabilities of the ASP view information. The Page.registerrequiresviewstateencryption method is to register the control as a control that requires view state encryption. If you are developing a custom control that handles potentially sensitive information, call the RegisterRequiresViewStateEncryption method to register the control with the page and ensure that the control's view-state information is encrypted.
The RegisterRequiresViewStateEncryption method must be called in the PreRender phase of the page life cycle or before that stage. The following code snippet, which overrides the OnPreRender method in the preceding Viewstatecontrol control and joins the View encryption feature, is as follows:
protected override void OnPreRender (EventArgs e)
{
This. Page.registerrequiresviewstateencryption ();
Base. OnPreRender (e);
}
Compiling the control and re-running it in the browser, you can see the value in the hidden domain view control that is sent to the client by:
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= "/ wepdwullte3nju0njm4otepzbycagmpzbyeagepdw8wah4ovgv4df9wawv3u3rhdgufiuaikeayr+ Euqfzpzxdtdgf0zewuuewzqowtmowcqoeahcfkfcsaahyehglcywnrq29sb3ikab4exyftqgiiz2qcbw8pfgiebfrlehqf9qn0aglzllzpzxdtdgf0zunvbnr yb2wxllrlehrftm9wawv3u3rhdgxlsz7mgkcg5rkh5pyj5l+d5a2y6keg5zu+ 54q25ocbidxjbwcgc3jjpsdjbwfnzxncslmuanbnjyavpiagpgjypjxicj50aglzllzpzxdtdgf0zunvbnryb2wxllrlehrfvmlld1n0yxrl5bge5ocniow3s ue7j+s/newtmos6huinhuwbvuektuaagsa8sw1nihnyyz0nsw1hz2vzxfhmlmpwzycglz4gidxicj48yni+ dghpcy5wawv3u3rhdgvdb250cm9sms5gywnlu3r5bgxlsz7mgkcut0sg5bey57up5l+d5a2y5lqg6keg5zu+ 54q25ocbidxjbwcgc3jjpsdjbwfnzxncwewuanbnjyavpiagpgjypjxicj50aglzllzpzxdtdgf0zunvbnryb2wxlkzhy2vtdhlszs5cywnrq29sb3ig5bey5 7up5l+d5a2y5lqg6keg5zu+54q25ocblcdnnqfvvizmijhnmotpopzoibllslhmmk/ kv53lrzjnmotpopzoibigpeltzybzcmm9j0ltywdlc1xytc5qcgcnic8+ ica8sw1nihnyyz0nsw1hz2vzxejhlmpwzycglz4gpgjypjxicj5kzgtfh8u37ehqnjzhtltcdxnah2mmfq== "/>
Becomes an already encrypted hash code:
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= "j62g77ydi1rdf8+ zxszw1d3eapnuzo8h2mcnpen2hr6daqnjllamfq4ehwwrrj16s6khfp43gwvpkmb9rpqtmai5gc+ lz9orqjszwpvavdtbi4ff6wfrj7qy1r2hregheqym/e1a+jx9oiwk1kus8vfj3snexckcddb7fqtr6dlcksbcggwdrawgiisi0o5pnymploa+ z74cly6/dyz23biaznnydlml/e7mvizss+v4fbzsxxbbdrx1owj60wjrfkscl1g0dpbn+ lz2niu7nh2t5xu8itw1nmjsfuoh9ymbmqkcnek3jaex18n9wbodlgsq8zxo/hagvqbcqtqstiuryezjvm8t9q0u9cj9si+ fns2pktvhxqh6qlsjh1dlwm+qfyjhk2fvbh+wa2nzuq3hzobr4gznfrlzn8zlio/fdjekyh+x9x7qneajs9dtg6mo/qbvi+s0w1viq/nj/ orc3poilwvwyd1wjblnd4b21rvjnoq/j+jpnqge+adqr2fw3kyatghkf6psvmum9fz26jy2rkyw2vbahpwmxhqnf9v5yt/ 0d3lp9mx7kyay4paitteezit4g5i0hp65a8d/qqjbqtcs1qz798mei66mqf8qrobxr2euss+ucekxqe+2xdhx2kkxntoqxgrj4/n8cdkr2z+ k3f3iemxobo+qvq5wutmwrskiy8jad562zcnbgjlyu72i+kuorhqcboxrgt5vpeibpdjb0vk3tb6t7g2t5yfyb7pbtqewv43ws4/ bpxysdr9ycnojd/tb6cdi5fus/dgytej2ezt2hopcm758weewbqzug+t1dfezu6+vicimhdv8ifcwnv+yhw== "/>
</div>
You can also specify the encryption mode for the view state information, which can be set by the system enumeration viewStateEncryptionMode, which controls whether the view state information is encrypted, and it has three enumerated items.
Enumeration member Description
Auto if the control requests encryption by calling the RegisterRequiresViewStateEncryption method, the view state information is encrypted, otherwise it is not encrypted. This value is the default value for the Page.viewstateencryptionmode property
Always forces the view state information to be encrypted, regardless of whether the call
Never view state information is not encrypted even if the control requests encryption
The above enumeration items, in addition to being set in the control, can also be configured at the application level in the configuration file or at the page level in page pages. When applied in a control or page, only the following settings are used:
This. Page.viewstateencryptionmode = Viewstateencryptionmode.auto;
This. Page.registerrequiresviewstateencryption ();
The page framework automatically completes the encryption and decryption functions as set above. Add two more points to this function:
(1) Encrypted view state will certainly affect the performance of the application, so use caution.
(2) Any view state in the page will be encrypted as long as any one of the controls in the page requires that the views state be encrypted. Currently, ASP. NET has not implemented a control view encryption feature in the PIN page (the method that invokes the page control page in the control or on the page is RegisterRequiresViewStateEncryption, and the method does not have any overloaded methods).