Basepage class + session General user logon permission Control

Source: Internet
Author: User

to determine whether a user is logged on, the common method is to use session control.
but many people prefer

 
Protected void page_load (Object sender, eventargs e ){}

To writeCodeOr even write in some buttons to determine whether a session exists ~~
Of course, this can achieve the effect. The problem is: if there are 1000 pages ~~ You need Ctrl + c... CTRL + V many times ~~~
My idea is to write a basepage class that inherits system. Web. UI. Page.

 

Code

  Public     Class  Basepage: system. Web. UI. Page
{
// The pageunload event does not mean that the browser is closed, but that the page is closed. Therefore, the following events are still executed during refresh.
Protected Void Page_unload ( Object Sender, eventargs E)
{

}

Protected Override Void Onpreinit (eventargs E)
{
Base . Onpreinit (E );
If ( ! Sessiondata. islogin ())
{ // Go to the logon page: for example:
Response. Redirect ( String . Format ( " ~ /Relogin. aspx? Page = {0} " , Request. Path ));
}}

 

Why do I need to include the page parameter here to return to the page before logon after successful logon


I also contributed a sessiondata class:

Code

  Using  System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using Expressplatform. Common;
Namespace Expressplatform. Web. appcode
{
Public Class Sessionkey
{
Public Const String Userinfo = " User " ;
}
/// <Summary>
/// Manage all session data in this class
/// </Summary>
Public Class Sessiondata
{
/// <Summary>
/// Obtain user information in a session
/// </Summary>
/// <Returns> </returns>
Public Static Mdlsessioncustomerinfo getuserinfo ()
{
Mdlsessioncustomerinfo userinfo = Sessionmanager < Mdlsessioncustomerinfo > . Getsessionobject (sessionkey. userinfo );
If (Userinfo = Null )
{
Userinfo = New Mdlsessioncustomerinfo ();
// Store content to applicationsProgram
Sessionmanager < Mdlsessioncustomerinfo > . Setsessionobject (sessionkey. userinfo, userinfo );
}
Return Userinfo;
}
/// <Summary>
/// Reset the user information in the session
/// </Summary>
/// <Param name = "userinfo"> </param>
Public Static Void Setuserinfo (mdlsessioncustomerinfo userinfo)
{
Sessionmanager < Mdlsessioncustomerinfo > . Setsessionobject (sessionkey. userinfo, userinfo );
}
/// <Summary>
/// Clear user information in the session
/// </Summary>
Public Static Void Clearuserinfo ()
{
Sessionmanager < Mdlsessioncustomerinfo > . Setsessionobject (sessionkey. userinfo, Null );
}
/// <Summary>
/// Login?
/// </Summary>
/// <Returns> </returns>
Public Static Bool Islogin ()
{
Bool RET = False ;
Mdlsessioncustomerinfo userinfo = Sessionmanager < Mdlsessioncustomerinfo > . Getsessionobject (sessionkey. userinfo );
If (Userinfo ! = Null )
RET = True ;
Return RET;
}
}
}

 

 

This article is original to me. It's too long for everyone to continue reading it here.

Http://asp.netfaq.cn/Coolite_ExtJs/thread-224-1-1.html

 
  Public ClassBasepage: system. Web. UI. Page

 

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.