A comparison of several methods in judging asp.net of session expiration--practical skills

Source: Internet
Author: User

Method One: The most troublesome and easiest way to think, in each page of the Page_Load () method to judge:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
if (session["username"]!= null)
{

                    //Login Success
                     Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "", "<script>alert (' Login successful! ') </script> ");
               }
                Else
                 {
                    //Expired, log back in
                     Response.Redirect ("loginform.aspx");

}
}
}

Disadvantages: Code redundancy, repeat write judgment session code.

Method Two: Rewrite the init () method in HttpModule, and then judge the session expiration.

1. Create a new class module that inherits the IHttpModule interface, allowing the module class to implement IHttpModule interface members.

2. Registers the AcquireRequestState event with the context in the Init () method.

3. In the AcquireRequestState method to implement the judgment session code.

Copy Code code 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 IHttpModule class, implement interface member
public class Module:ihttpmodule
{
#region IHttpModule Members

public void Dispose ()
{
throw new NotImplementedException ();
}

2. Registers the AcquireRequestState event with the context in the Init () method.
public void Init (HttpApplication context)
{
Context. Acquirerequeststate+=new EventHandler (context_acquirerequeststate);
}

3. Perfect AcquireRequestState method, then judge session expiration
public void Context_acquirerequeststate (object sender, EventArgs e)
{
HttpApplication app = (HttpApplication) sender;

if (app. context.session["username"] = = null)
{
App. Response.Write ("<script>alert" (' Session expires!) ');</script> ");
}
}

#endregion
}
}

4. Configure the Web.config file and add the following code to the <system.web>

Copy Code code as follows:


<!--rewrite the IHttpModule class, you need to configure the information-->
<add name= "Demo" type= "Judgesessionouttime.module"/>
<!--type is followed by a namespace. Class name-->

Advantages: High efficiency, no redundancy code, one configuration, full benefit.

Principle: The class module that implements the IHttpModule interface is executed before the page is executed. That is, before the Page_Load () event executes, the module is executed, then the method of judging the session is executed, does not expire, continues execution, expires, performs the action, and does not have to do the Page_Load () page.

Sentiment: To tell the truth, for this, in doing Web site login, and then judge the user name aspect, I think not very suitable, because, once the program starts loading, will execute module class method, then session or empty, so no matter how, will not put down go, has stopped in the login interface (personal opinion, You are welcome to shoot bricks! )

Mode three: Override the OnInit () virtual method of the inherited page, and inherit the class on the desired interface.

1. Create a new class judgesession that inherits the page class, implementing interface members.

2. Rewrite the OnInit () method to determine the session status.

3. On the page where you need to determine the expiration of the session, inherit the Judgesession class, rather than the page class, to achieve the effect.

Copy Code code 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 ("Session does not expire, user name:" +session["username"]. ToString ());
}
}

}
}

Advantages: The method is flexible and the code reuse rate is high. In the need to judge the session of the page to inherit the Judgesession class, unwanted pages, inherit the page class can be.

Related Article

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.