But many people like
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{}
To write code, or 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.
Copy codeThe Code is as follows:
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 ())
{// Enter here to jump to the login page: for example:
Response. Redirect (string. Format ("~ /ReLogin. aspx? Page = {0} ", Request. Path ));
}}
Why should I include the Page parameter here to return to the previous Page after successful logon?
In addition, I also contributed a SessionData class:
Copy codeThe Code is as follows:
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>
/// Data in all sessions, managed in this class
/// </Summary>
Public class SessionData
{
/// <Summary>
/// Obtain the user information in the session
/// </Summary>
/// <Returns> </returns>
Public static MdlSessionCustomerInfo GetUserInfo ()
{
MdlSessionCustomerInfo userInfo = SessionManager <MdlSessionCustomerInfo>. GetSessionObject (SessionKey. UserInfo );
If (userInfo = null)
{
UserInfo = new MdlSessionCustomerInfo ();
// Store the content to the application
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 the user information in the session
/// </Summary>
Public static void ClearUserInfo ()
{
SessionManager <MdlSessionCustomerInfo>. SetSessionObject (SessionKey. UserInfo, null );
}
/// <Summary>
/// Whether to log on
/// </Summary>
/// <Returns> </returns>
Public static bool IsLogin ()
{
Bool ret = false;
MdlSessionCustomerInfo userInfo = SessionManager <MdlSessionCustomerInfo>. GetSessionObject (SessionKey. UserInfo );
If (userInfo! = Null)
Ret = true;
Return ret;
}
}
}
Copy codeThe Code is as follows:
Public class BasePage: System. Web. UI. Page