Presentation is loading. Please wait.

Presentation is loading. Please wait.

Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.

Similar presentations


Presentation on theme: "Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig."— Presentation transcript:

1 Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig

2 Maintaining State  Tools: 1. Cookies 2. Viewstate 3. Session 4. Cache

3 1. Cookies  Small text files stored on user’s computer  Useful for storing: Date of last visit Date of last visit Unique user identifier Unique user identifier Preferences Preferences etc. etc.  Limitations Not secure – user can view, modify, delete Not secure – user can view, modify, delete User can disable User can disable

4 1. Cookies  Write: Single value per cookie: Single value per cookie: Response.Cookies[“Name”].Value = “Bart”;Response.Cookies[“Name”].Value = “Bart”; Multiple values per cookie Multiple values per cookie Response.Cookies[“Name”][“First”] = “Bart”;Response.Cookies[“Name”][“First”] = “Bart”; Response.Cookies[“Name”][“Last”] = “Simpson”;Response.Cookies[“Name”][“Last”] = “Simpson”;  Read Request.Cookies[“Name”].Value; Request.Cookies[“Name”].Value; Request.Cookies[“Name”][“First”].Value; Request.Cookies[“Name”][“First”].Value;

5 1. Cookies  Expiration: Default: when browser is closed Default: when browser is closed Response.Cookies[“CookieName”].Expires = DateTime.Now.AddDays(180); Response.Cookies[“CookieName”].Expires = DateTime.Now.AddDays(180); Delete Cookie: set expiration to past (-1) Delete Cookie: set expiration to past (-1)

6 1. Cookies  Testing for cookie Attempt to read a cookie that is not present: Attempt to read a cookie that is not present: Error: Object reference not set to an instance of an object  Solution: If (Request.Cookies[“Name”] != null) { //safe to read cookie name = Request.Cookies[“Name”].Value; } name = Request.Cookies[“Name”].Value; }

7 1. Cookies  Benefits: Persist between sessions Persist between sessions Keep track of usernames, last visit, etc. Keep track of usernames, last visit, etc. Easy to use Easy to use  Drawbacks: Client can block Client can block Not secure Not secure

8 2. Viewstate  Data in web controls automatically stored Encoded in hidden form field Encoded in hidden form field  May add to viewstate programmatically ViewState[“UserID”] = “333”; ViewState[“UserID”] = “333”;  Retrieve with same syntax Returned datatype is object Returned datatype is object Must convertMust convert int intUserID = Convert.ToInt32(ViewState["UserID"]); int intUserID = Convert.ToInt32(ViewState["UserID"]);

9 ViewState  Benefits: Very convenient Very convenient Can access data even when controls hidden Can access data even when controls hidden  Drawback Difficult to transfer between pages Difficult to transfer between pages Does not persist between sessions Does not persist between sessions

10 3. Sessions  Data stored on server  Unique to each user  Uses cookie to associate with user User must have cookies enabled User must have cookies enabled  Create: Session[“LastName”] = “Simpson”; Session[“LastName”] = “Simpson”;  Delete Session.Abandon; – Deletes the session Session.Abandon; – Deletes the session Session.Remove[“LastName”]; – removes items Session.Remove[“LastName”]; – removes items

11 Sessions  Expiration Default: 20 minutes Default: 20 minutes Session.Timeout = 60; Session.Timeout = 60;  Benefits Secure Secure Client cannot view, edit, deleteClient cannot view, edit, delete Automatic timeout Automatic timeout  Drawbacks Do not persist Do not persist Require cookies Require cookies Use server resources Use server resources

12 4. Cache  Stored on server Application Application  Often used to cache data Example: XML Music Example: XML MusicXML MusicXML Music  Store any type of data / object Stored as type “object” Stored as type “object” Recast when retrieved Recast when retrieved

13 Cache  Add item to cache: Cache.Insert(“key”, object, dependency, absoluteExpiration, slidingExpiration) Cache.Insert(“key”, object, dependency, absoluteExpiration, slidingExpiration)  Example: Cache.Insert(“cat30”, “apples”, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration) Cache.Insert(“cat30”, “apples”, null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration)

14 Cache  Retrieve: If (Cache[“cat30”] != null) { string fruit = (string) Cache[“cat30”]; string fruit = (string) Cache[“cat30”];}

15 Cache  Benefits: Secure Secure Many expiration options Many expiration options Store any object Store any object  Drawbacks: ?? ??

16 When to use each: CookiesNeed data to persist across sessions Security not important ViewstateData associated with page Similar to hidden form field SessionSecure Associated with each user session CacheSecure Share data across sessions Control expiration Example: output (source)output source

17 Summary  Maintaining state: Method depends on needs Method depends on needs Several flexible alternatives Several flexible alternatives


Download ppt "Maintaining State MacDonald Ch. 9 MIS 324 MIS 324 Professor Sandvig Professor Sandvig."

Similar presentations


Ads by Google