Session and State Management
Session and State Management
Management
1
Company Confidential
Introduction
• Session state
– is maintained on a per-client basis.
– When a client first accesses any page in an application, an
ASP.NET generated session ID is created.
– That session ID is then transmitted between the server
and the client via HTTP either using client-side cookies or
encoded in a mangled version of the URL.
• Cookies
– provide the ability to store small amounts of data on a
client's machine.
– Once a cookie is set, all subsequent pages accessed by
the same client will transmit the cookie and its value.
State Management (Cont…)
• Cookie :
– A cookie is a small file on the user’s computer that
contains information specific to one web site.
– This file can contains things such as username and
passwords that will be used to customize a user’s visit
to the site.
– Cookies can contain any simple data type such as
string, integer, floats, Booleans, and so on.
– For example many sites that display news headlines
will allow users to select which types of news they
want to see . This information can be stored in
cookies so that the next time the user visits, the site
can read those values and customize accordingly.
The http cookie object
0:30 100 200 The first 100 sessions are still active weather or not the
visitors are still on the site. 100 new visitors means 100 new
sessions.
1:00 100 300
Another 100 visitors adds 100 new session while the first two
group sessions still have not expired.
• Session (Cont…)
Do Don’t
Do use session variable s if Don’t use session variables
you have a small amount of when you have a lot of
information for a single user information to store for each
that you need to maintain for user. There are other
the current session such as a methods that won’t eat up
user name or password. your server’s memory as
fast, such as cookies and
databases.
Using Session Variables
function Session_OnStart ( )
{
// you must lock the global Application
object
// when writing to it - ok to read without
lock
Application.Lock ( );
for(int n=0;n<cookies.Count;n++)
{
HttpCookie cookie = cookies[n];
Response.Write("<hr/>Name: <b>" + cookie.Name + "</b><br />");
Response.Write("Expiry: " + cookie.Expires + "<br />");
Response.Write("Address1: " + cookie.Address1+ "<br />");
Response.Write("Address2: " + cookie.Address2+ "<br />");
Response.Write("City: " + cookie.City+ "<br />");
Response.Write("Zip: " + cookie.Zip+ "<br />");
}
Creating cookies
Again notice the difference between accessing a value and key/ value
pairs– specifically, the use of the value property.
Using Cookies (Cont…)
• Cache Object
– By using caching we can speed up processing
– There were also third-party options, like XCache
– The main benefits of caching are performance-related:
operations like accessing database information can be
one of the most expensive operations.
– If the database information is fairly static, this
database-information can be cached
– When information is cached, it stays cached either
indefinitely, until some relative time, or until some
absolute time
Using ASP.NET Output Caching
Classification : Confidential