Академический Документы
Профессиональный Документы
Культура Документы
Overview
State Management Application and Session Variables Cookies and Cookieless Sessions
What is State Management? Types of State Management Server-Side State Management Client-Side State Management
Web Server
Web Server
Session state
Database
Query strings
In some cases, use database support to maintain state on your Web site
Application state is a global storage mechanism accessible from all pages in the Web application Session state is limited to the current browser session
Values are preserved through the use of application and session variables Scalability
Client Computer
Less secure than server-side state management options Limited amount of information
Web Server
Client Computer
Cookies
Only one Global.asax file per Web application Stored in the virtual root of the Web application Used to handle application and session events The Global.asax file is optional
ASP.NET Web Server IIS ASP.NET HTTP Runtime Application_BeginRequest Application_AuthenticateRequest Application_AuthorizeRequest Application_ResolveRequestCache Application_EndRequest Application_UpdateRequestCache
Application_AquireRequestState
Application_PreRequestHandlerExecute
Application_ReleaseRequestState
Application_PostRequestHandlerExecute
Page execution
Initializing Application and Session Variables Using Application and Session Variables Demonstration: Using Session Variables Application and Session Variable Duration
The Application object shares information among all users of a Web application
Sub Application_Start(s As Object,e As EventArgs) Application("NumberofVisitors") = 0 End Sub protected void Application_Start(Object sender,EventArgs e) { Application["NumberofVisitors"] = 0; }
Session("BackColor") = "blue" Application.Lock() Application("NumberOfVisitors") += 1 Application.UnLock() Session["BackColor"] = "blue"; Application.Lock(); Application["NumberOfVisitors"] = (int)Application["NumberOfVisitors"] Application.UnLock();
+ 1;
Initialize a session variable (a number) in global.asax Access the session variable from one page Access the session variable from another page and modify it Re-access the session variable from the first page
Default is 20 minutes
Not Scalable State can be stored in a SQL Server database or a state server
State server
Scalable
OSQL creates several stored procedures and temporary databases for storing the variables
Using Cookies to Store Session Data Instructor-Led Practice: Using Variables and Cookies Retrieving Information from a Cookie Using Cookieless Sessions
Creating a cookie:
HttpCookie objCookie = new HttpCookie("myCookie"); HttpCookie DateTime now objCookie = DateTime.Now; = new HttpCookie("myCookie"); DateTime now = DateTime.Now; objCookie.Values.Add("Time", now.ToString()); objCookie.Values.Add("Time", now.ToString()); objCookie.Values.Add("ForeColor", "White"); objCookie.Values.Add("ForeColor", "Blue"); objCookie.Values.Add("BackColor", "White"); objCookie.Values.Add("BackColor", "Blue"); objCookie.Expires = now.AddHours(1); Response.Cookies.Add(objCookie); To create a persistent Response.Cookies.Add(objCookie);
cookie, specify the expiration time
Students will:
See how the application and session variables, and persistent cookies, are used to store user information
Time: 15 Minutes
lblTime.Text = objCookie.Values("Time") lblTime.ForeColor = System.Drawing.Color.FromName _ (objCookie.Values("ForeColor")) lblTime.BackColor = System.Drawing.Color.FromName _ (objCookie.Values("BackColor")) lblTime.Text = objCookie.Values["Time"]; lblTime.ForeColor = System.Drawing.Color.FromName (objCookie.Values["ForeColor"]); lblTime.BackColor = System.Drawing.Color.FromName (objCookie.Values["BackColor"]);
Session IDs are communicated across client-server requests using an HTTP cookie or included in the URL
Cookieless sessions
http://server/(h44a1e55c0breu552yrecobl)/page.aspx
Session state is configured in the <SessionState> section of Web.config Set cookieless = true
Review
State Management Application and Session Variables Cookies and Cookieless Sessions
Page Header
Menu Component
Class1.vb or Class1.cs Header.ascx
ASPState
Registration
Register.aspx
Web. config
tempdb
Life Insurance
Life.aspx
Retirement
Retirement.aspx
Medical
Medical.aspx
Dental
Dental.aspx
Prospectus
Lab Web Application
Prospectus.aspx
Doctors
Doctors.aspx
User Control
namedate.ascx
XML Files
Doctors
Dentists