CSState Management
CSState Management
Agenda
View state
Application cache
Session state
Profiles
Cookies
View State
Mechanism for persisting relatively small
pieces of data across postbacks
Used by pages and controls to persist state
Also available to you for persisting state
Relies on hidden input field (__VIEWSTATE)
Accessed through ViewState property
Tamper-proof; optionally encryptable
Reading and Writing View State
// Write the price of an item to view state
ViewState["Price"] = price;
Web Server
Created with
ASP.NET ASPState
ASPState
ASP.NET InstallSqlState.sql or
Database
Database InstallPersistSql-
State.sql
Session Events
Session_Start event signals new session
Session_End event signals end of session
Process with handlers in Global.asax
void Session_Start ()
{
// Create a shopping cart and store it in session state
// each time a new session is started
Session["Cart"] = new ShoppingCart ();
}
void Session_End ()
{
// Do any cleanup here when session ends
}
Session Time-Outs
Sessions end when predetermined time
period elapses without any requests from
session's owner
Default time-out = 20 minutes
Time-out can be changed in Web.config
<!-- Web.config -->
<configuration>
<system.web>
<sessionState timeout="60" />
...
</system.web>
</configuration>
Profile Service
Stores per-user data persistently
Strongly typed access (unlike session state)
On-demand lookup (unlike session state)
Long-lived (unlike session state)
Supports authenticated and anonymous users
Accessed through dynamically compiled
HttpProfileBase derivatives (HttpProfile)
Provider-based for flexible data storage
Profile Schema
Profiles
HttpProfileBase
HttpProfileBase
HttpProfile
HttpProfile(Autogenerated
(Autogenerated HttpProfile
HttpProfile(Autogenerated
(Autogenerated
HttpProfileBase-Derivative)
HttpProfileBase-Derivative) HttpProfileBase-Derivative)
HttpProfileBase-Derivative)
Profile Providers
AccessProfileProvider
AccessProfileProvider SqlProfileProvider
SqlProfileProvider Other
OtherProviders
Providers
Other
Access SQL Server
Data Stores
Defining a Profile
<configuration>
<system.web>
<profile>
<properties>
<add name="ScreenName" />
<add name="Posts" type="System.Int32" defaultValue="0" />
<add name="LastPost" type="System.DateTime" />
</properties>
</profile>
</system.web>
</configuration>
Using a Profile
// Increment the current user's post count
Profile.Posts = Profile.Posts + 1;
Cookie name
Cookie value
Reading a Cookie
HttpCookie cookie = Request.Cookies["UserName"];
if (cookie != null) {
string username = cookie.Value; // "Jeffpro"
...
}
© 2003-2004 Microsoft Corporation. All rights reserved.
This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.