Lesson 1: Saving and Retrieving User Information
The purpose of this lesson is to introduce the concept of user customization features in ASP.NET. User-specific information can be stored on the client as cookies and/or on the server in an XML file managed by ADO and a predefined XML schema. Since this review is written "close" to the next release of Visual Studio, it may help to mention that ASP.NET 2.0 expands on this concept profoundly with a rich set of user customization features.
- "The advantage of using cookies is that the interaction happens invisibly to the user... The disadvantage of using cookies is that users can set their browsers not to accept cookies."
This lesson suggests two ways to design with cookies: store all information in the cookie, being aware of the 4096-byte limit or store a GUID in the cookie which is used to retrieve user, server data. In either case, the design features a check for client-support of cookies with the Boolean in Request.Browser.Cookies and, optionally, setting the expiration date of the cookies with the DateTime.Now.AddDays() method.
- "Cookies are case sensitive..."
This implies that Response.Cookies["lastVisit"] is not equal to Response.Cookies["LastVisit"].
- "By default, cookies expire when the user's session ends. Setting Expires to the DateTime.MaxValue means that the cookie never expires."
Remove a cookie by setting the Expires property to DateTime.Now.
- "To store user information on the server instead of the client's machine, simply use cookies as an identification device... The Microsoft .NET Framework provides the System.Guid namespace for creating globally unique identifiers (GUIDS)."
The code sample demonstrates:
HttpCookie cookie = (Request.Cookies["userID"] != null) ?
Request.Cookies["userID"]
: new HttpCookie("userID",System.Guid.NewGuid.ToString());
- "Data sets provide the Find method to retrieve rows of data by unique keys... Using a data set means that the file you create on the server must be either a database or an XML file."
Visual Studio .NET provides XML and XSD editors to create XML files for use with DataSet objects when the formality of a database seems unnecessary. Defining a schema with an ID element that will store our GUID, allows us to use the DataSet.ReadXmlSchema() method to automatically create a DataTable that is compatible with our external XML file.
- "When you've created an XML file and an XML schema, you can read those files into a data set using the ReadXmlSchema and ReadXml methods, respectively... To save user information on the server as an XML file, use the DataSet object's SaveXML method."
The "SaveXML method" does not exist among the members of DataSet, so evidently this is an editorial error. The code sample that follows this error clearly suggests that we use the DataSet.WriteXML() method.