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