Identity authentication is often performed during ASP. NET development. It is troublesome to add verification code to each page. 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.
| The code is as follows: |
Copy code |
|
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)
{
// Obtain the 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:
| The code is as follows: |
Copy code |
Public partial class demo: System. Web. UI. Page
|
Changed:
| The code is as follows: |
Copy code |
|
Public partial class demo: 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.