Using SQL Server for ASP.NET Session State

xiaoxiao2021-03-06  51

Before Using Any Sample Code Published Here Please Read The Disclaimer.

What is session state?

A session is defined as the Period of Time That A Web Application. Session State Is a Collection of Objects, Tied To A Session Are Stored On A Server.

Why use sql?

Once you start running multiple web servers for the same web site, the default asp.net session state, InProc, is no longer useful, as you can not guarantee that each page request goes to the same server. It becomes necessary to have a central state Store That Every Web Server Accesses.

SQL Server offers you centralized storage of a session state in a Web farm. It also offers the transactional capabilities that provide reliability to most relational database systems. You can use SQL Server to save a session. This process is not as efficient as InProc and StateServer mode, because you must store the information in a different process or on a different server. However, this option may be more efficient than using the aspnet_state service, depending on the actual workload and the database configuration. Once you start saving session state to a SQL Database It Will Also Persist Through Web Server Restarts and reboots.

For Reliability You Should Consider Storing Session State for A Web Farm on A SQL Cluster.

Creating the database

Start Query Analyzer, connected to the server you want to use for state storage Open and execute InstallSqlState.sql script file By default, InstallSqlState.sql is located in one of the following folders;.. System drive / WINNT / Microsoft.NET / Framework / version / system drive / Windows / Microsoft.NET / Framework / version / If you are using trusted connections to connect to your server, you must change ownership of the state database to sa after creation. In Query Analyzer run use ASPStateexec sp_changedbowner 'sa ',' true 'If you are using SQL authentication create a user and password for session state to use. At a minimum this user should havepermissions to execute the stored procedures in the ASPState database. you will have to manually set these, or if you 'Refeing Dangerous, Give the State User Dbo Rights To AspState.configuring ASP.NET

To switch asp.net to use sql you must update the Element of Your Application's Web.config File As Follows;

Set The Mode Attribute of The Element to SQL Server. Set The Sqlconnectionstring Attribute To Specify THE Connection String To your SQL Server

For example

<

SessionState

Mode

= "SQLServer"

SqlConnectionstring

= "Data Source = Server; user ID = uid; password = pwd"

Cookieles

= "false"

TIMEOUT

= "20"

/>

If you specify integrated security / trusted connections in the database connection string (ie. "Trusted_connection = true", or "integrated security = sspi"), you can not use impersonation in asp.net, as your database connection will then run the context of the impersonated user, which will not have rights to the state database. you can, of course grant connections to that user context. KB 326606 has more details.If you are configuring session state to be stored on a cluster you must manually override the. Net Machine Keys on Each Server. KB 323262 HAS More Details.

Configuring SQL SP3

So, you have created the database, you've created the user, given it rights to all the stored procedures in the ASPState database, you've up to date with patches, Windows, IIS SQL, and you connect to your web site. .

SELECT permission denied on object 'ASPStateTempApplications', database' tempdb ', owner' dbo'.INSERT permission denied on object 'ASPStateTempApplications', database' tempdb ', owner' dbo'.SELECT permission denied on object 'ASPStateTempApplications', database' tempdb ', Owner' DBO '.

SP3 for SQL 2000 Adds a New Security Feature, Disabled by Default, Cross-Database Ownership Chaining (See Feature IS Disabled, ASP.NET SESSION Stops Working.

To Reconfigure SQL 2000 SP3 for ASP.NET Session State You Must Run

USE

Master

Go

EXEC

SP_Configure

'

Cross DB Ownership Chaining

'

,

'

0

'

;

Reconfigure

Go

Now Restart Your SQL Server, Then Run

USE

Master

Go

EXEC

sp_dboption

'

AspState

'

,

'

DB chaining

'

,

'

True

'

Go

Timeouts under heavy loadIf your web servers are under heavy load it may be useful to increase the time out for session state access. You can add the stateNetworkTimeout attribute to the sessionState settings in web.config and machine.config

<

SessionState

STATENETWORKTIMEOUT

= "15"

/>

IF A Web Server or A State Server IS Under Stress and Cannot Complete Session Accesses on Time, Event ID 1072 And Event ID 1076 May Be Logged In The Event Log.

Cavets When Using SQL Session State

Using SQL IS SLOWER THANING Inproc session state. When Storing Basic Data Types (String, Int, ETC), ASP.NET CAN Take 10% -25% Longer to Store Their Values. Complex Types Take Even Longer. Of Course Because You Are Connecting to a Separate Server It Does Use Bandwidth on your network.

When using SQL Server mode, objects stored in session state are serialised and deserialised when a request is processed. So any objects which do not support serialisation can not be stored in session state. In ASP.Net v1.0 a bug means that attempting to store A Non-Serialisable Object Does Not throw an error, And so will probably pass unnoticed.

For session state to be maintained across different web servers in a web farm (the main reason for moving session state to SQL), the Application Path of the website (For example / LM / W3SVC / 2) in the IIS Metabase should be identical in For all the web servers in the Web Farm. Microsoft's KB 325056 Details this qu.

If you wish to Persist session State Through SQL Server Reboots You Must Follow The Instructions in KB 311209 To Move The Session State Tables from Tempdb To The AspState Database.

Other Resources

Peter Bromberg's Session State FAQKB 317604 HOW TO: Configure SQL Server to Store ASP.NET Session StateNote that a few of the Microsoft KB articles show sample code which uses SA as the username and a blank password As the SQL Slammer worm showed, this is. NOT A Good Idea!

转载请注明原文地址:https://www.9cbs.com/read-115507.html

New Post(0)