Go Asp. NET sessions (session) Save mode

Source: Internet
Author: User
Tags goto

This article transferred from: http://blog.csdn.net/cityhunter172/article/details/727743

author : Han Yu feng (cityhunter172)

Hello everyone, have not written anything for four months. Today, let's talk about the Session's 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, total Off, InProc, StateServer, Four modes of SQL Server. OFF, InProc refers to "do not enable", "in-process Save (default)", the two modes have nothing to say, the so-called InProc is to save the Session in aspnet_wp.exe (Windows 2000 parsing ASP. NET page) or w3wp.exe (Win2003 process), the session will be lost once the process has been aborted or reset.

First, raised Session several reasons for the loss

People who have moved through the handwriting code know that it is more common to lose the Session. The following is what I have encountered in the past few years, 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 terminated in Task Manager or in other cases.

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

4. InProc mode: Web. config modified

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

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

All of these are InProc modes that are prone to cause parsing of an ASP. NET application reset. Does it feel Wohuo? Before I had this feeling, I got used to it, and then I didn't use it. So, there is the use of the following two modes of the attempt, is now written to share with you.

Second, Use StateServer Save Session

The essence of the StateServer pattern is that the session is stored in a separate process, independent of the aspnet_wp.exe or w3wp.exe. When this service is enabled, you can see a process named Aspnet_state.exe in the Task manager, starting with the specific steps to set up:

1, modify the registry (key steps, such as)

Run regedit→ open registry → locate hkey_local_machine/system/currentcontrolset/services/aspnet_state/parameters node → Set the key value of Allowremoteconnection to "1" (1 for Allow, 0 for prohibit) → Set port (port number)

Precautions:

A) if the ASP. NET State service is running and you modify the registry content, you will need to restart the service

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

c), when the key value of Allowremoteconnection is set to "1", it means to allow the connection of the remote computer, that is, as long as you know your service port, you can enjoy your ASP. NET State Service, that is, the session is stored in your computer process, So please use with caution; When the key value is "0", only the stateConnectionString is "tcpip=localhost:42424" and "tcpip=127.0.0.1:42424", you can use the ASP. Service

2. Open the ASP. NET state Service (e.g.)

Right click on "My Computer" → management → services and applications → services → Double-click "ASP." → Start (can be set to "Auto")

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

3. Change the 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

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

Precautions:

A), after setting into StateServer, must have corresponding stateconnectionstring

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

Third, will be Session put SQL Server Save

The SQL Server mode is to store the session in the SQL Servers database (note that it is not Oracle, the reason why the toes can be guessed), let's start by explaining the specific steps of the setup:

1. Start the related database service ()

Run SQL Server Service Manager → start SQL Server (preferably on boot autorun) → Start the SQL Server Agent service (preferably set to power on automatically)

Precautions:

A), note the boot sequence, or set it up by right-clicking on "My Computer" → management → services and applications → services → find "MSSQLSERVER" and "SQLServerAgent" → Start and set the startup type to "Auto"

b) The role of SQL Server agent here is to clear the expired Session in the database

2, the establishment of the DataBase to store the Session

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

3. Establish a user to connect to the database ASPState and authorize 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 that tempdb only retains access to the SA account after the database is started, and that the remaining account permissions are cleared, but this database is needed for the save session;

A), run SQL Server Enterprise Manager → expand database security → right-click login → new login → Enter name → select "SQL Server Authentication" → enter "password" → specify "database" → click "Database access" → tick " ASPState "→ check" db_owner "role → click" OK "→ once again enter" password "→ Click" OK "to establish ASPState user (here to establish a test user named" Sessionstateuser "with password" 123456 ")

B), run SQL Server Enterprise Manager → expand administration → expand SQL Server Agent → Right click on "Jobs" → Click "New Job" → Enter "name" (this example is grantsessionuser) → Click the label "Steps" → New → Enter "step name" (this example is Grant01) → Select database "tempdb" → write SQL script "Execsp_adduser ' sessionstateuser ', ' sessionuser ', ' db_owner '" → OK → points Click label "schedule" → new → enter "name" (this example is Start01) → select type "SQL Server agent starts automatically when startup" → OK → click "OK" to finish adding

C), you can also run the following script one-time to deal with the above a, B two steps

/****** Script starts ******/

--New Database account Sessionstateuser, default login ASPState

EXEC sp_addlogin ' sessionstateuser ', ' 123456 ', ' ASPState '

Use ASPState--switch DataBase

--Grant Sessionstateuser permissions to the db_owner

exec sp_adduser ' sessionstateuser ', ' sessionuser ', ' db_owner '

Use master--switch DataBase

BEGIN TRANSACTION

/****** declaring variable ******/

DECLARE @JobID BINARY (16)

DECLARE @ReturnCode INT

SELECT @ReturnCode = 0

--If not, add a category for the job

IF (SELECT COUNT (*) from msdb.dbo.syscategories WHERE name = N ' [Uncategorized (Local)] ') < 1

EXECUTE msdb.dbo.sp_add_category @name = N ' [Uncategorized (Local)] '

--New job

EXECUTE @ReturnCode = msdb.dbo.sp_add_job--Call stored procedure Sp_add_job

@job_id = @JobID OUTPUT,--assigns the returned JobID to the variable

@job_name = N ' Grantsessionuser ',--job name

@owner_login_name = NULL,--default to all current user

@description = NULL,

@category_name = N ' [Uncategorized (Local)] ',--job classification attribution

@enabled = 1,--whether enabled

@notify_level_email = 0,

@notify_level_page = 0,

@notify_level_netsend = 0,

@notify_level_eventlog = 0,

@delete_level = 0

if (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO quitwithrollback--Rollback On Error

--New step

EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep--Call stored procedure Sp_add_jobstep

@job_id = @JobID,--incoming JobID newly created

@step_id = 1,

@step_name = N ' Grant01 ',--step name

@command = N ' exec sp_adduser ' sessionstateuser ', ' sessionuser ', ' db_owner ',

--SQL script to be executed (note that single quotes in SQL are represented by two consecutive single quotes)

@database_name = N ' tempdb ',--the database used to execute the above SQL

@server = N ",

@database_user_name = N ",

@subsystem = N ' TSQL ',--the execution type is "Transact-SQL script"

@cmdexec_success_code = 0,

@flags = 0,

@retry_attempts = 0,

@retry_interval = 1,

@output_file_name = N ",

@on_success_step_id = 0,

@on_success_action = 1,

@on_fail_step_id = 0,

@on_fail_action = 2

IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback

--New schedule

EXECUTE @ReturnCode = Msdb.dbo.sp_add_jobschedule

@job_id = @JobID,

@name = N ' Start01 ',--Dispatch name

@enabled = 1,

@freq_type = 64--"64" means to run when the SQLServerAgent service starts

IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback

--Add a new job to the local database

EXECUTE @ReturnCode = Msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name =n ' (local)

IF (@ @ERROR <> 0 OR @ReturnCode <> 0) GOTO Quitwithrollback

COMMIT TRANSACTION

GOTO Endsave

Quitwithrollback:

IF (@ @TRANCOUNT > 0) ROLLBACK TRANSACTION

Endsave:

/****** Script End ******/

4. Setting up Web. config content

Open web.config→ find <sessionState> node content → Modify to the following content:

<sessionstate mode= "SQL Server" sqlConnectionString = "Data source=192.168.0.2; User Id=sessionstateuser; password=123456 "timeout="/>

Precautions:

A), initial Catalog option cannot appear in sqlConnectionString

b) The role of SQL Server agent here is to clear the expired Session in the database

c), if you skip the third step, the user ID needs to be logged in with SA

D), if sqlconnectionstring as "data source=127.0.0.1; Trusted_connection=yes, log on to the database using the local computer ASPNET (Windows 2000 system account) or Network Service (Windows 2003 System account). If the database does not allow the above users to log on, then the error is also, even if the above account can successfully log on, but to assign their tempdb permissions, the reason is that the Session is saved in tempdb, if the database is not access permission is not drop. See:

Cold Feather Maple ( cityhunter172 )

2006-05-14 00:01 finalized

Go Asp. NET sessions (session) Save mode

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.