Solution to session loss in Asp.net (2)

Source: Internet
Author: User
Publisher: [notes...] Source: [site] Browse: [206] Comment: [0]

    Session loss in Asp.net may be due to the following reasons:

    1. Check for timeout. The default value is 20 minutes. The setting method is session. Timeout = 30;
    2. Check whether the Code contains session. Abandon () or the like.
    3. The ACCESS database is used in ASP. NET, and the database is placed in the bin directory. The solution is not to put the updated files in the bin directory.
    4. 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.
    5. 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.
    6. 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)
    7. Asp.net default configuration, causes of inexplicable session loss and Solutions
    (Original article http://www.cnblogs.com/supersand/archive/2006/05/20/404919.html)
    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:
    Because the Asp.net program is configured by default, the session settings in the web. config file are as follows:
    <Sessionstate mode = 'inc' stateconnectionstring = 'tcpip = 127.0.0.1: 42424 'sqlconnectionstring = 'data source = 127.0.0.1; trusted_connection = Yes 'cookieless = 'true' timeout = '60'/>
    The sessionstate label has an attribute mode. It can have three values: inproc and StateServer? Sqlserver (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:
    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 see the service named ASP. Net state service in the system service. It is not started by default. 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. The session will not be lost unless the computer restarts or the stateservice breaks down (it is normal that the session is discarded due to session timeout ).
    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, where there is an IP address, the default is the local machine (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 information on how to use sqlserver to save sessions, visit here.
    When you use StateServer or sqlserver to store a session, all objects to be saved to the session must be serialized except the basic data type (default data type, such as int and string. You only need to put the [serializable] label before the class to be serialized.

    So far, I have solved the problem by using the mode = "stateservice". In addition, note that stateconnectionstring = 'tcpip = 127.0.0.1: 42424 '. This attribute alone is not acceptable, still error, also caused by <sessionstate mode = 'inproc' stateconnectionstring = 'tcpip = 127.0.0.1: 42424 'sqlconnectionstring = 'data source = 127.0.0.1; trusted_connection = Yes 'cookieless = 'true' timeout = '60'/>

    It must be complete!

So far, I have solved the problem by using the mode = "stateservice". In addition, note that stateconnectionstring = 'tcpip = 127.0.0.1: 42424 '. This attribute alone is not acceptable, the error persists. It must be completed!

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.