Professional Documents
Culture Documents
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
Application Cache
Intelligent in-memory data store
Item prioritization and automatic eviction Time-based expiration and cache dependencies Cache removal callbacks
Cache.Insert
public void Insert ( string key, // Key that identifies item object value, // The item itself CacheDependency dependencies, // Cache dependencies (if any) DateTime absoluteExpiration, // When should item expire (absolute)? TimeSpan slidingExpiration, // When should item expire (sliding)? CacheItemPriority priority, // Item's priority relative to other items CacheItemRemovedCallback onRemoveCallback // Removal callback delegate );
Temporal Expiration
Cache.Insert ("Stocks", stocks, null, DateTime.Now.AddMinutes (5), Cache.NoSlidingExpiration);
Expire after 5 minutes Expire if 5 minutes elapse without the item being retrieved from the cache
Cache Dependencies
Cache.Insert ("Stocks", stocks, new CacheDependency (Server.MapPath ("Stocks.xml")));
Expire if and when Stocks.xml changes Expire if and when the "Stocks" database's "Prices" table changes
Application Cache
Session State
Read/write per-user data store Accessed through Session property
Page.Session - ASPX HttpApplication.Session - Global.asax
Cookied or cookieless
Web Server
ASP.NET Session State
Web Server
ASP.NET
State Server
aspnet_state Process
Web Server
ASP.NET
Database Server
ASPState Database
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 HttpProfile (Autogenerated HttpProfileBase-Derivative) HttpProfile (Autogenerated HttpProfileBase-Derivative)
Profile Providers
AccessProfileProvider SqlProfileProvider Other Providers
Access
SQL Server
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; // Update the current user's last post date Profile.LastPost = DateTime.Now;
Profile Groups
Properties can be grouped <group> element defines groups
<profile> <properties> <add ... /> ... <group name="..."> <add ... /> ... </group> </properties> </profile>
Profile Providers
Profile service is provider-based Beta 1 ships with two providers
AccessProfileProvider (Access)* SqlProfileProvider (SQL Server)
Profiles
Cookies
Mechanism for persisting textual data
Described in RFC 2109 For relatively small pieces of data
HttpCookie class encapsulates cookies HttpRequest.Cookies collection enables cookies to be read from requests HttpResponse.Cookies collection enables cookies to be written to responses
HttpCookie Properties
Name
Name Value Values HasKeys Domain
Description
Cookie name (e.g., "UserName=Jeffpro") Cookie value (e.g., "UserName=Jeffpro") Collection of cookie values (multivalue cookies only) True if cookie contains multiple values Domain to transmit cookie to
Expires
Secure Path
Creating a Cookie
HttpCookie cookie = new HttpCookie ("UserName", "Jeffpro"); Response.Cookies.Add (cookie);
Reading a Cookie
HttpCookie cookie = Request.Cookies["UserName"]; if (cookie != null) { string username = cookie.Value; // "Jeffpro" ... }