Identity Authentication is often performed during ASP. NET development. It is troublesome to add verification for each page.Code. We know that ASP. net pages are inherited from system. web. UI. page class, we can customize a class, let it inherit from the page class, and add verification operations in this class. Then let the page class to be verified inherit the custom class to implement the unified verification function.
1. Create a base class basepage for page verification. This class inherits from the page class.
Using System; Using System. Data; Using System. LINQ; Using System. Collections. Generic; Using System. Web; Using System. Web. UI; Using System. Web. Security; Using System. Web. UI. webcontrols; Using System. Web. UI. webcontrols. webparts; Using System. Web. UI. htmlcontrols; /// <Summary> /// All page base classes to be verified /// </Summary> Public Class Basepage: system. Web. UI. Page { /// <Summary> /// Constructor /// </Summary> Public Basepage (){ // How to add a load event This . Load + = New Eventhandler (basepage_load );} // Perform verification during page loading Void Basepage_load ( Object Sender, eventargs e ){ // Get session If ( Object . Equals (session [ " Userid " ], Null )){ // Jump to the login page This . Response. Redirect ( " ~ /Login. aspx " );}}}
2. All interfaces to be verified are inherited from the basepage class.
Create a new page demo. aspx. If the page requires verification, change the base class of the page from page to basepage:
Public Partial ClassDemo: system. Web. UI. Page
Changed:
Public Partial ClassDemo: basepage
After the modification, when the demo. ASPX page is loaded, it will detect the userid In the session. If it is null, it will jump to the registration logon interface. The pages to be verified will be inherited from the basepage class.