Determine whether the session has expired:
Implemented through basrpage or ihtttpmoudle
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 ");
}
<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 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:
<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 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:
<% Systemdriver %>/winnt/Microsoft. NET/framework/<% version %>/
To configure the SQL Server server, you can run the SQL server command line tool osql.exe in the command line.
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:
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.