Several methods for processing ASP. NET Session invalidation

Source: Internet
Author: User

Check ASP. whether the NET Session expires or not. The default value is 20 minutes. The setting method is Session. timeOut = 30; or you can set it to a higher value. The unit of the value is minute, depending on the situation.

Check whether the Code contains Session. Abandon () or the like. Once the Abandon method is called, the current session is no longer valid and a new session is started.

ASP. NET uses the ACCESS database, and the database is placed in the bin directory. The solution is not to put the updated files in the BIN directory.

Only when a session is uploaded from the same page to another page can the session be continued. That is to say, the Session cannot be cross-origin. When the client closes the browser or closes the browser session, the session disappears, A new Session will be created when you access the server again, but the server will survive and wait for timeout, but the call will not be made. in addition, for example, set the SESSION on the first page, and then REDIRECT the second page. The solution is to set endResponse to FALSE in REDIRECT.

Cause of Iframe Session loss: the session is jointly authenticated by the client and the server. The client storage ID is sent to the server by the hair appended to the page for identification by the server, if the conditions are met, the corresponding session operation permission can be obtained.

However, if the page is from the framework, and the parent page and framework of the framework are not a site, the client will not attach header information to the page by default, in this way, the server cannot identify the pages in the client framework and cannot operate sessions. Solution: Response. AddHeader ("P3P", "CP = cao psa our") 'or start asp.net State Service in the Window Service.

Some antivirus software will scan your Web. Config file. At that time, the Session will definitely drop. This is Microsoft's statement. Solution: Enable the antivirus software to mask and scan the Web. Config file (do not edit it when the program is running)

Causes and solutions for inexplicable Session loss under ASP. NET Default Configuration

Under normal operation, the Session will be lost for no reason. Because the program is constantly being operated, the possibility of Session timeout is ruled out. In addition, the Session timeout time is set to 60 minutes, and will not time out so quickly.

Now I will write down the cause and solution.

Cause of ASP. NET Session failure:

Because the asp.net program is configured by default, the Session settings in the Web. Config file are as follows:

 
 
  1. <sessionState mode='InProc' stateConnectionString='tcpip=127.0.0.1:42424' 
  2. sqlConnectionString='data source=127.0.0.1;Trusted_Connection=yes' cookieless='true' 
  3. timeout='60'/> 

The sessionState label has an attribute mode. It can have three values: InProc and StateServer? SQLServer is case sensitive ). The process is unstable. When some events occur, the process restarts, causing the loss of sessions stored in the process.

Under what circumstances will the process be restarted? An article by Microsoft tells us:

1. memoryLimit attribute of the processModel label in the configuration file

2. The Global. asax or Web. config file is changed.

3. The Web program DLL in the Bin folder is modified.

4. Anti-Virus Software scanned some. config files.

For more information, see PRB: Session variables are lost intermittently in asp.net applications.

Solution to ASP. NET Session failure:

In the sessionState label mentioned above, the mode attribute can have three values: StateServer and SQLServer. The two Session types are both external, so when aspnet_wp.exe is restarted, the Session will not be affected.

Set the mode to StateServer. StateServer is a Service on the local machine. You can view the Service named asp.net State Service in the system Service. By default, the Service is not started. After we set the mode to StateServer, manually start the service.

In this way, we can use the StateService of the Local Machine to store sessions. Unless the computer restarts or the StateService breaks down, it is normal that the Session will not be lost because the Session times out and is discarded ).

In addition, sessions can be saved through StateService on other computers. The specific modification is as follows. Also, in the sessionState label, there is a stateConnectionString = 'tcpip = 127.0.0.1: 8080' attribute, which has an IP address. The default value is 127.0.0.1 ), you can change it to the IP address of the computer that runs the StateService service as you know, so that the asp.net program located on different computers can communicate with each other.

If you have higher requirements and the Session is not lost when the service period is restarted, you can set the mode to SQLServer and modify the sqlConnectionString attribute. For the operation of saving Sessions Using SQLServer, when using StateServer or SQLServer to store sessions, all objects to be saved to the Session except the default data types of basic data types, such as int and string) must be serialized. You only need to put the [Serializable] label before the class to be serialized.

For example:

 
 
  1. [Serializable]  
  2. public class MyClass  
  3. {  
  4.     ......  
  5. } 0 0 0 
  1. ASP. NET array basics: declarations, representations, and examples
  2. Top 10 skills in ASP. NET Programming II)
  3. Top 10 skills in ASP. NET Programming 1)
  4. ASP. net mvc tutorial (1): Preparations
  5. Deployment of ASP. net mvc Beta

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.