When I wrote a customer's B/S structure application, I suddenly found a trick and did not know whether it was a BUG in MS, if you want to write a Session check class to a friend who has studied it, you must switch to the login page after the Session fails. Each page that calls this class can be in a different Web path, therefore, the URLs for transferring to the login page are different. Every page must call and set the login page path. Therefore, the actual application gave up this idea and later considered that it is better to write a check ASP. NET Session failure page, the client refresh every second, you can call it in a page, but the requests are sent when the ASPX is embedded through the FRAME, which is not very nice, although the page is hidden. later, let's think about it. We just need to use a page, and use the refreshing technology to request the page with the expired Session. The refreshing technology here uses the XMLhttp object instead of the WebService technology. (because the customer's BS system uses the FRAME framework, the top page displays the software name and user login information, so the call is placed on this page ).
Let's take a look at the code, mainly the client's JavaScript script program.
- <Script language ="Javascript">
- Var idx = 0;
- Function ChkSession ()
- {
- Var Http =NewActiveXObject ("Microsoft. XMLHTTP");
- Http. open ("GET","ChkSessionOut. aspx",False);// Check the ASP. NET Session failure page
- Http. send ();
- Var str = Http. responseText;// Result returned after ASPX execution
- // Idx ++
- // Document. all ("ConvertResult"). innerHTML = str + idx;
- If(Str ="Notnull")
- {
- // Alert (str );
- }
- Else
- {
- Alert ("The session value tracing time has timed out. Please log on again ...");// This code has never been run. You can see it later.
- Location. href ="Longin. aspx";
- }
- Window. setTimeout ('chksession () ', 1000 );// Request ChkSessionOut. aspx every second
-
-
- }
- </Script>
-
This CODE is placed between the <HEAD> </HEAD> labels and called when the BODY is loaded as follows:
- < BODY onload="ChkSession();">
The ChkSessionOut. aspx. cs code is as follows: I deleted all the HTML tags in the ChkSessionOut. aspx file. In this way, the following code is executed and only the result is output.
- private void Page_Load(object sender, System.EventArgs e)
- {
- if(Session["sUserID"]==null)
- {
- Response.Write("isnull");
- return;
- }
- else
- {
- Response.Write("notnull");
- return;
- }
- }
-
To test the above program, I changed the content of WEB. Config, changed the SESSION setting segment to a part of the WEB. Config file that expires one minute later, and set the location where the SESSION expires one minute later, as shown below:
- < sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="1"/>
Then I run the code. I used the original IE to open a new IE window using the menu. To ensure that they are checking the same SESSION, enter ChkSessionOut in the address. aspx, after the program runs, the secret is found. One minute later, I will refresh ChkSessionOut. the aspx page actually outputs notnull. I think, by the way, I am sending requests to this page every second, so there is no reason for the failure. Then I will close the page with no refreshing new technologies, one minute later, I was surprised to find that the output isnull indicates that the Session is invalid! Think of VS.net well, it is a good tool, my environment IIS5.0, Win2000,. net2003 then I set up 30 seconds of automatic requests. The homepage of the brushless newest is not closed, and the SESSION is not invalid. If the SESSION is disabled or one sub-type, it becomes invalid. Considering that the value of one minute/30 seconds is an integer, it is set to 50 seconds, and the main page of the brushless new technology is not closed. It has passed one species and becomes invalid. Because 1 minute/50 seconds is not an integer, it is invalid.
In this way, the ASP. NET Session becomes invalid.
- Brief Introduction to ASP. NET Session Model
- ASP. NET page lifecycle: stages, events, and others
- Introduction to ASP. net mvc Lifecycle
- Several methods for processing ASP. NET Session invalidation
- ASP. NET: all instances share a static variable.