Implement unified ASP. NET identity authentication using custom classes

Source: Internet
Author: User

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.

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.