Asp.net inherits the page class override method to implement the most basic user login authentication permission verification.

Source: Internet
Author: User

Today, when I was looking at the development of the Asp.net Server Control, I saw that all of the above is based on countrol inheritance, and then rewrite the method to achieve some basic effects.

As a result, I will inherit the page class and rewrite its method to achieve the most basic problems such as determining whether a user is logged on or not during page loading.

 

The following is the simplest way to determine whether a user is logged on.

CodeAs follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; </P> <p> namespace webapplication1 <br/>{< br/> public class roots: page <br/>{< br/> protected override void oninit (eventargs e) <br/>{< br/> If (session ["username"] = NULL) <br/> {<br/> response. redirect ("login. aspx "); </P> <p >}< br/> base. oninit (E); <br/>}</P> <p >}< br/>

 

I wrote a roots class, inherited the page, and then rewritten the oninit method (for details about the page lifecycle, refer to here)

Then you can inherit the roots class on the page you want to verify.

For example, Main. aspx

Public partial class main: roots <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{</P> <p >}< br/>

When your tag is empty, the logic in your oninit statement is executed.

Of course, many general functions can be implemented through rewriting, such as the master style and different logon permissions of different users.

I am here to discuss the issue. I hope to learn from you.

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.