Method 1: the most troublesome and easy-to-think method, which can be determined in the page_load () method of each page:
Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Session ["username"]! = Null)
{
// Successful Login
Page. ClientScript. RegisterClientScriptBlock (this. GetType (), "", "<script> alert ('logon successful! ') </Script> ");
}
Else
{
// Expired, log on again
Response. Redirect ("LoginForm. aspx ");
}
}
}
Disadvantage: Code redundancy. Repeat the writing to determine the session code.
Method 2: override the Init () method in HttpModule and determine the session expiration status.
1. Create a class Module that inherits the IHttpModule interface and enable the Module class to implement IHttpModule interface members.
2. register the AcquireRequestState event on the Context in the Init () method.
3. The session code is judged in the AcquireRequestState method.
Copy codeThe Code is as follows: using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Namespace JudgeSessionOutTime
{
// 1. inherit the IHttpModule class and implement interface members
Public class Module: IHttpModule
{
# Region IHttpModule Member
Public void Dispose ()
{
Throw new NotImplementedException ();
}
// 2. register the AcquireRequestState event in the Init () method.
Public void Init (HttpApplication context)
{
Context. AcquireRequestState + = new EventHandler (context_AcquireRequestState );
}
// 3. Improve the AcquireRequestState method and determine whether the session expires.
Public void context_AcquireRequestState (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;
If (app. Context. Session ["username"] = null)
{
App. Response. Write ("<script> alert ('session expired! '); </Script> ");
}
}
# Endregion
}
}
4. Configure the web. config file and add the following code to <system. web>:
Copy codeThe Code is as follows:
<! -- Rewrite the IHttpModule class and the information to be configured -->
<Add name = "demo" type = "JudgeSessionOutTime. Module"/>
<! -- Type is followed by namespace. Class Name -->
</HttpModules>
Advantages: high efficiency, no code redundancy, one configuration, full use.
Principle: The class Module implementing the IHttpModule interface is executed before the execution page. That is, before the page_load () event is executed, the Module is executed, and then the session method is judged to be executed. If the session method is not expired, the corresponding operation is executed, you do not need to execute the page_load () PAGE method.
Sentiment: To be honest, I think this is not suitable for website login and user name judgment, because once the program starts to load, it will execute the module class method. At that time, the session is still empty, so no matter what you do, you won't let it go, and you will never stop on the logon page (for personal opinions, please take a look !)
Method 3: override the OnInit () virtual method that inherits the page and inherit the class on the required interface.
1. Create a class JudgeSession that inherits the page class to implement interface members.
2. Rewrite the OnInit () method to determine the session status.
3. inherit the JudgeSession class instead of the page class on the page for judging the expiration of the session to achieve the effect.
Copy codeThe Code is as follows: // JudgeSession class
Using System;
Using System. Data;
Using System. Configuration;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Namespace JudgeSessionOutTime
{
Public class JudgeSession: System. Web. UI. Page
{
Protected override void OnInit (EventArgs e)
{
If (Session ["username"] = null)
{
Response. Write ("session expired! ");
}
Else
{
Response. Write ("the session has not expired. username:" + Session ["username"]. ToString ());
}
}
}
}
Advantages: flexible methods and high code reuse rate. Inherit the JudgeSession class on the page to determine the session. If you do not need a page, inherit the page class.