Professional Documents
Culture Documents
Managing State
Managing State
Web Server
Client Computer
Application and Session variables
SessionID
Client-Side State Management
Cookies
The Global.asax File
• Only one Global.asax file per Web application
Session("BackColor") = "blue"
Application.Lock()
Application("NumberOfVisitors") += 1
Application.UnLock()
Session["BackColor"] = "blue";
Application.Lock();
Application["NumberOfVisitors"] =
(int)Application["NumberOfVisitors"] + 1;
Application.UnLock();
strBgColor = (string)Session["BackColor"];
lblNbVisitor.Text = Application["NumberOfVisitors"].ToString();
Application and Session Variable Duration
<configuration>
<system.web>
<sessionState timeout="10" />
</system.web>
</configuration>
Web farm
-Or-
SQL
Client
<sessionState mode="SQLServer"
sqlConnectionString="data source=SQLServerName;
Integrated security=true" />
• Cookieless Sessions
• Creating a cookie:
HttpCookie
HttpCookie objCookie
objCookie == new
new HttpCookie("myCookie");
HttpCookie("myCookie");
HttpCookie
DateTime now
HttpCookie
DateTime objCookie
now = DateTime.Now;
objCookie = new
= DateTime.Now;HttpCookie("myCookie");
= new HttpCookie("myCookie");
DateTime now = DateTime.Now;
DateTime now = DateTime.Now;
objCookie.Values.Add("Time",
objCookie.Values.Add("Time", now.ToString());
now.ToString());
objCookie.Values.Add("Time",
objCookie.Values.Add("ForeColor",now.ToString());
objCookie.Values.Add("ForeColor", "White");
objCookie.Values.Add("Time", now.ToString());
"White");
objCookie.Values.Add("ForeColor",
objCookie.Values.Add("BackColor",
objCookie.Values.Add("BackColor", "White");
"Blue");
objCookie.Values.Add("ForeColor", "Blue");
"White");
objCookie.Values.Add("BackColor", "Blue");
objCookie.Values.Add("BackColor", "Blue");
objCookie.Expires
objCookie.Expires == now.AddHours(1);
now.AddHours(1);
Response.Cookies.Add(objCookie);
Response.Cookies.Add(objCookie);
Response.Cookies.Add(objCookie);
Response.Cookies.Add(objCookie);
To create a persistent
cookie, specify the
expiration time
• Cookie contains information about the domain name
Set-Cookie:
Set-Cookie: Username=John+Chen;
Username=John+Chen; path=/;
path=/;
domain=microsoft.com;
domain=microsoft.com;
Expires=Tuesday,
Expires=Tuesday, 01-Feb-05
01-Feb-05 00.00.01
00.00.01 GMT
GMT
Retrieving Information from a Cookie
lblTime.Text = objCookie.Values["Time"];
lblTime.ForeColor = System.Drawing.Color.FromName
(objCookie.Values["ForeColor"]);
lblTime.BackColor = System.Drawing.Color.FromName
(objCookie.Values["BackColor"]);
Cookieless Sessions
http://server/(h44a1e55c0breu552yrecobl)/page.aspx
Cannot use absolute URLs
Most browsers limit the URL size to 255 characters, which
limits use of cookieless Session IDs
Setting Up Cookieless Sessions
Logon information
Virtual machine 2310C_14
User name Student
Password Pa$$w0rd
Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config
XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• How do you set up a Web application to use cookieless
sessions?
• What are the three categories of events that are handled
in the global.asax file?
• Where is the global.asax file of an application located?