. NET Session loss solution and cause analysis

Source: Internet
Author: User
Tags access database

. NET Session loss solution and cause analysis

Determine whether the Session has expired:
Implemented through BasePage or IHtttpMoudle
Because the Asp.net program is configured by default, the Session settings in the Web. Config file are as follows:

<SessionState mode = 'inproc' stateConnectionString = 'tcpip = 127.0.0.1: 8080'

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 ). By default, the Session is stored in the process (IIS5 is

Asp net_wp.exe, and iis6is w3wp.exe). The process is unstable. When some events occur, the process restarts.

The Session stored in the process is lost.

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.

Public class BasePage: System. Web. UI. Page
    {
Public BasePage ()
      {

      }

Protected override void OnInit (EventArgs O)
      {
If (base. Session ["UserId"] = null | base. Session ["UserId"]. ToString

(). Equals (""))
          {
Response. Redirect ("~ /Error. aspx ");
          }
      }
    }

If (Session ["user"] = null)
{
This. Response. Redirect ("Error. aspx ");
}
Asp.nettutorial-session-lost.html "> url: http://greatverve.cnblogs.com/archive/2011/06/28/asp-net-session-lost.html

<CustomErrors mode = "RemoteOnly" defaultRedirect = "Error. aspx">
</CustomErrors>
Session loss solution summary:

1 <sessionState mode = "InProc" cookieless = "false" timeout = "60"> </sessionState>
This can solve the timeout problem, but this is extremely insecure. vs.net is set by default.
This is equivalent to handing over a session to the IIS process for management. IIS instability will cause frequent Session loss.
 
2 sessionState mode = StateServer is a service process management mode that establishes an independent Session,
This is not affected by the instability of IIS processes, and it is not easy to cause session loss,
In this way, session sharing can be realized for multiple asp.net sites.

If the mode attribute is set to StateServer in the web. config file, you must start the management tool-service-ASP. NET

State service item. You must also set the IP address and port number of the StateServer server.

If StateServer is run on the machine, the IP address is 127.0.0.1, and the port number is usually 42424. The configuration is as follows:
<SessionState mode = "StateServer"
StateConnectionString = "tcpip = 127.0.0.1: 42424" ......>
In this mode, session state storage does not depend on the failure or restart of the IIS process, and session status is stored in

The memory space of the StateServer process.

3 SQLServer mode. This mode saves the session status in the SQL Server database tutorial. Use this mode

You must have at least one SQL Server and create the required tables and stored procedures on the Server .. NET SDK

Two scripts are provided to simplify this work: InstallSqlState. SQL and UnInstallSqlState. SQL. The two countries

The file is stored in the following path:

<% SYSTEMDRIVER %> WinntMicrosoft. NETFramework <% version %>
To configure the SQL Server, you can run the command line tool provided by SQL Server in the command line.

Osql.exe
Osql-s [server name]-u [user]-p [password] <InstallSqlState. SQL
For example, osql-s (local)-u as-p ""-I InstallSqlState. SQL
After necessary database preparation, modify the mode attribute of the sessionstate element in the web. config file.

Is "sqlserver" and specifies the SQL connection string. The details are as follows:
Mode = "SQLServer"
SqlConnectionString = "data

Source = 127.0.0.1; userid = sa; password =; Trusted_Connection = yes"
When SQL server mode is used, the Session status does not depend on the IIS server. You can also use SQL

Server cluster, so that the state storage does not rely on a single SQL Server, this can provide a great deal

Reliability


Summary of asp.net Session loss

Working Principle of Session in asp:

Asp sessions are process-dependent. ASP Session status is stored in the IIS process, that is

Inetinfo.exe. When the inetinfo.exe process crashes, the information is lost. In addition, restart

Or, disabling the IIS service will cause information loss.

Implementation of asp.net Session

The Session of asp.net is based on the HttpModule technology. The HttpModule can handle the request before the request is processed.

Status control, because the Session itself is used for state maintenance, so the HttpModule is used for Session

This is not suitable.

Cause 1:

The files in the bin directory are rewritten. asp.net has a mechanism to ensure that the system runs normally after the dll is re-compiled.

It will restart a website process, which will lead to Session loss, so if an access database is located in

Bin directory, or other files are rewritten by the system, which will cause Session loss.

Cause 2:

In the folder option, if the "open folder window in a separate process" is not opened, once a new window is created

It may be considered as a new Session and cannot access the original Session. Therefore, you must enable this option. Otherwise

Cause Session loss

Cause 3:

It seems that most of the Session loss is caused by the client, so you have to start from the client to see if the cookie has been opened.

Cause 4:

Is there a problem with the Session time settings? Will it be lost due to timeout?

Cause 5:

The maximum number of cookies in IE (20 cookies per domain) may cause session loss.

Cause 6:

The web garden mode and InProc mode are used to save the session

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.