Asp.net session expiration or loss Solution

Source: Internet
Author: User

Let's share an article about how to use a session that suddenly encounters session loss for no reason. My code is no different from other ones, but this is the case. Let's take a look at my solution.

Whether the Session disconnection expires:
Implemented through BasePage or IHtttpMoudle

 

The Code is as follows: Copy code

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 ");
}
Url: http://www.bKjia. c0m

<CustomErrors mode = "RemoteOnly" defaultRedirect = "Error. aspx">
</CustomErrors>


Session loss solution summary:

1

The Code is as follows: Copy code
<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 runs on the machine where IIS is located, the IP address is 127.0.0.1, and the port number is usually 42424. The configuration is as follows:
  

The Code is as follows: Copy code
<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 the session state is stored in the memory space of the StateServer process.

3 SQLServer mode. This mode saves the session Status in the SQL Server database. Before using this mode, you must have at least one SQL Server and create the required tables and stored procedures in the Server .. . Net sdk provides two scripts to simplify this work: InstallSqlState. SQL and UnInstallSqlState. SQL. Files in these two countries are stored in the following path:

 

The Code is as follows: Copy code
<% SYSTEMDRIVER %> WinntMicrosoft. NETFramework <% version %>

To configure the SQL Server, you can run the SQL Server command line tool osql.exe in the command line.
  

The Code is as follows: Copy code
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, change the mode attribute of the sessionstate element in the web. config file to "sqlserver" and specify the SQL connection string. The details are as follows:
  

The Code is as follows: Copy code
Mode = "SQLServer"
SqlConnectionString = "data source = 127.0.0.1; userid = sa; password =; Trusted_Connection = yes"

In SQLServer mode, the Session state is independent from the IIS Server, and the SQL Server cluster can be used to make the status storage independent from a single SQL Server, in this way, the application can be highly reliable.

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.