Asp. NET sessions (session) Save mode __.net

Source: Internet
Author: User
Tags sessions

Source: http://blog.csdn.net/cityhunter172/article/details/727743

Take the time to speak today. Storage mode in. Net v1.0/v1.1. You can search for < sessionstate > in MSDN 2003 to see a description of the < sessionstate > node elements in Web.config, a total of off, InProc, StateServer, S Qlserver four different modes. Off, InProc respectively refers to "not enabled", "in-process storage (default)", the two modes are not said, the so-called InProc is to save the session in aspnet_wp.exe (Windows 2000 parsing ASP. NET page (process) or w3wp.exe (Win2003 process), the session will be lost once the process is aborted or reset.

One, causes the session to lose several reasons

People who have moved the handwriting code know that session loss is more common. The following is my several years encountered, can cause the session lost reason, dare not say is hundred percent, the probability of loss is particularly high. Wrong ..., can be said to be "phase ... When ... "High wow ^_^"

1, storage session of the computer restart (nonsense, if this is not lost, you immortal AH)

2. InProc mode: aspnet_wp.exe or w3wp.exe causes its process to be aborted in Task Manager or otherwise.

3, InProc mode: After the modification of the. cs file, compiled two times (compile only once, sometimes not lost)

4, InProc mode: Modified Web.config

5, InProc mode, Windows 2003 Environment: Application pool recycle, reboot after stop

6. InProc mode: the. dll file in the bin directory on the server is updated

All of the above are listed in the InProc mode, easy to cause resolution asp.net application reset. Don't feel very wohuo. I have this feeling before, slowly get used to it, and then simply do not use this mode. As a then, there is an attempt to use the following two patterns, which are written to share with you.

second, use StateServer to save session

The essence of the StateServer model is to store the session in a separate process, which is independent of aspnet_wp.exe or w3wp.exe. After you enable this service, you can see a process named Aspnet_state.exe in Task Manager, starting with the specific steps for setting:

1, modify the registry (key steps, as shown below)

Run regedit→ open registry → Find Hkey_local_machine/system/currentcontrolset/services/aspnet_state/parameters Node → Set the key value of Allowremoteconnection to "1" (1 is allowed, 0 is forbidden) → Set port (port number)

Precautions:

A), if the ASP.net state service is running and the registry content is modified, the service needs to be restarted

(b) Note that the key value of the port number is stored in hexadecimal and can be modified using decimal, 42424 is the default port

C, the Allowremoteconnection key value is set to "1", meaning that the remote computer connection is allowed, that is, as long as you know your service port, you can enjoy your ASP.net state service, that is, the session stored in your computer process, So please use caution; When the key value is "0", only the case where the stateconnectionstring is "tcpip=localhost:42424" and "tcpip=127.0.0.1:42424" can be used before the ASP.net state Service

2, open asp.net state Service (pictured below)

Right-click "My Computer" → admin → service and application → service → Double-click "asp.net state Service" → Start (can be set to "automatic")

Description: This service is available as long as the. Net Framework v1.0/v1.1 is installed.

3. Change Web.config

Open web.config→ find <sessionState> node content

<sessionstate

Mode= "InProc"

Stateconnectionstring= "tcpip=127.0.0.1:42424"

sqlconnectionstring= "Data source=127.0.0.1; Trusted_connection=yes "

Cookieless= "false"

Timeout= "/>"

→ Change it to the following content

"StateServer" stateconnectionstring= "tcpip=192.168.0.2:42424" timeout= "<sessionstatemode="/>

Precautions:

A), when set to StateServer, must have a corresponding stateconnectionstring

b, note IP address (can be remote computer IP, computer name, domain name) and port number, the port number needs to be consistent with the service port of the ASP.net State service

third, put the session into SQL Server save

SQL Server mode is where the session is stored in a SQL Server database (note that it is not Oracle, the toes can be guessed), and here's how to set up the steps:

1, start the relevant database services (as shown)

Run SQL Server Service Manager → start SQL Server (preferably at power-on autorun) → Start SQL Server Agent service (preferably set to run automatically)

Precautions:

A), note the boot sequence can also be set by: Right click on "My Computer" → admin → service and application → service → find "mssqlserver" and "SQLServerAgent" → Start and set Startup type to "Automatic"

b, the SQL Server agent is here to clear the expired session in the database

2, the establishment of the database to store the session

Run SQL Query Analyzer → Log on to the database using "sa" or a user with db_owner permissions of "master" → Open the query file c:/winnt/microsoft.net/framework/v1.1.4322/ InstallSqlState.sql (to be found in the. Net installation directory of the Windows system directory) → Run the SQL script directly → refresh the database to see the DB named ASPState

3, establish a connection to the database ASPState users, and authorization for this user (this step can be skipped)

The reason for this is that you do not want to have the sa password in Web.config, and the second is that tempdb only retains the use of an SA account after the database is started, and the rest of the account's permissions are cleared, but this database is needed to save the session.

A), Enterprise Manager running SQL Server → expand database security → right-click login → new login → Enter name → choose SQL Server authentication → Enter password → specify "database" → click "Database access" → tick "ASPState" → Check "db_owner" role → click "OK" → once again enter the "password" → Click "OK" to establish aspstate users (here to establish a "sessionstateuser", the password is "123456" test users)

B, Enterprise Manager running SQL Server → expand management → expand SQL Server Agent → Right click job → click "New Job" → Enter "name" (This example is

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.