Programming idea of ASP. NET Session failure

Source: Internet
Author: User

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.

 
 
  1. <Script language ="Javascript">
  2. Var idx = 0;
  3. Function ChkSession ()
  4. {
  5. Var Http =NewActiveXObject ("Microsoft. XMLHTTP");
  6. Http. open ("GET","ChkSessionOut. aspx",False);// Check the ASP. NET Session failure page 
  7. Http. send ();
  8. Var str = Http. responseText;// Result returned after ASPX execution 
  9. // Idx ++ 
  10. // Document. all ("ConvertResult"). innerHTML = str + idx; 
  11. If(Str ="Notnull")
  12. {
  13. // Alert (str ); 
  14. }
  15. Else 
  16. {
  17. Alert ("The session value tracing time has timed out. Please log on again ...");// This code has never been run. You can see it later. 
  18. Location. href ="Longin. aspx";
  19. }
  20. Window. setTimeout ('chksession () ', 1000 );// Request ChkSessionOut. aspx every second 
  21.  
  22.  
  23. }
  24. </Script>
  25.  

This CODE is placed between the <HEAD> </HEAD> labels and called when the BODY is loaded as follows:

 
 
  1. < 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.

 
 
  1. private void Page_Load(object sender, System.EventArgs e)  
  2. {  
  3.    if(Session["sUserID"]==null)  
  4.    {  
  5.     Response.Write("isnull");  
  6.     return;  
  7.    }  
  8.    else 
  9.    {  
  10.     Response.Write("notnull");  
  11.     return;  
  12.    }   
  13. }  
  14.  

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:

 
 
  1. < 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.

  1. Brief Introduction to ASP. NET Session Model
  2. ASP. NET page lifecycle: stages, events, and others
  3. Introduction to ASP. net mvc Lifecycle
  4. Several methods for processing ASP. NET Session invalidation
  5. ASP. NET: all instances share a static variable.

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.