ASP. NET to save session in database

Source: Internet
Author: User
Tags session id

1. Because the project DLL file changes more frequently, and save the status of the landing in the session, so that users often do not drop the line. (The session is lost due to DLL changes)

2. There is a way to save the session for a long time, that is the session SQL Server mode. Let's start by introducing the four modes of the session:

Asp. NET session state module in the Web. config file <System.web> tag under the <Sessionstate> tag's mode property to determine the four possible values of the property: Off, Inproc StateServer and SQL Server.

①. InProc is the default setting

It allows a "no cookie" session and stores session data outside the server. Asp. NET session state module is configured in the Web. config file as follows: <sessionstate mode= "InProc" cookieless= "false" timeout= "/>" in this example,     The Mode property is set to InProc (the default), which indicates that the session state is to be stored in memory by ASP and does not pass the session ID without a cookie. Instead, the session ID is inserted directly into the query string of a Web page URL. For example, after using InProc mode and establishing a session, when invoking an imaginary ASP. NET page, you need to use the following url:http://my.website.com/(12MFJU55VGBLUBJLWSI4DGJQ)/         Education.aspx long letters and numeric strings in parentheses are the session IDs. Asp. NET engine extracts the session ID from the query character and links the user request to a specific session. In this way, no cookie or hidden form fields are unnecessary. So, even if you don't use a form in your Web page, you can join a conversation. But this way, the state of the application will depend on the ASP. NET process, the state saved in the process is lost when the IIS process crashes or a normal restart occurs.

②.Mode property set to Off

Like the previous ASP, ASP. NET session state management is to incur overhead. Therefore, if a webpage does not need to access the session object, the developer should set the page precompiled Directive's EnableSessionState property to False.   To disable session state for the entire Web site, you can set the Mode property of the sessionstate element to off in the Web. config file. To overcome the drawbacks of the InProc model, ASP. NET provides two methods for saving session state outside of the process (③ and ④).

③.StateServer Session Management

Set the Mode property to StateServer, which is to store session data in a separate memory buffer, and then control the buffer by a Windows service running on a separate machine. The State service full name is the "ASP. Aspnet_state.exe", which is configured by the stateConnectionString property in the Web. config file.    This property specifies the server on which the service resides, and the ports to monitor: <sessionstate mode= "StateServer" stateconnectionstring= "tcpip=myserver:42424" Cookieless= "false" timeout= "/>" In this example, the State service runs on a 42424 port (the default port) of a machine called MyServer. To change the ports on the server, edit the port value in the Hklm\system\currentcontrolset\services\aspnet_state registry key. Obviously, the advantage of using a state service is process isolation and can be shared in a Web farm. With this mode, session state storage will not depend on the failure or restart of the IIS process, however, all session data will be lost once the state service is aborted. In other words, the State service does not store data as long as SQL Server does; it simply stores the data in memory.

④. session Management with SQL Server (FOCUS)

Asp. NET also allows session data to be stored in a database server by turning the Mode property into SQL. In this case, ASP. NET attempts to store session data in the SQL Server specified by the sqlConnectionString property, which contains the data source and the security credentials required to log on to the server.

3. Configuring session Management with SQL Server

The specific configuration is as follows:

①, run the Framewor installation directory InstallSqlState.sql file, the Framework of the installation directory for the c:/windows/microsoft.net/framework/... (Example: C:\Windows\Microsoft.NET\Framework\v4.0.30319)

②, the error will be prompted because the app is not registered. At this point you need to register the app under the command line and run the following command:

Aspnet_regsql.exe-ssadd-sstype c-d <database name>-S <sql Server ip>-u <user name>-P <Password >

Note: <database name> is a database named ASPState, <sql Server ip> is a DB instance name like Ibm-pc\sqlexpress (if the database is not a 2005 write-off IP address, the connection fails), <user name> is the password for SA (or equivalent to SA),<password> as the SA user name

The session definition succeeds, but prompts for the appropriate configuration in the Web app, and looking at SQL Server will find that the database aspstate is increased, but there are no tables.

③, run the following command at the command line:

Aspnet_regsql.exe-ssadd-sstype p-s <sql Server ip>-u <user name>-P <Password>

The command persisted for this application. At this point you will see the ASPState database with two more tables, aspstatetempsession can be used to save the session.

When applying, you need to add the following configuration in Webconfig:

The configuration nodes are as follows:
<sessionstate mode= "SQL Server" timeout= "sqlconnectionstring=" server=127.0.0.1;uid=sa;pwd=1234; " ></sessionState>

As follows:

Although timeout is set to 20 minutes, you can still get the value of the session after it expires.

Original address: http://blog.csdn.net/aiouwen521/article/details/17790681

ASP. NET to save session in database

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.