ASP. NET custom classes implement unified authentication program code

Source: Internet
Author: User

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.

 

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.