Session|session
Note that you need to refer to the System.Runtime.Serialization.Formatters.Soap.dll assembly
Public Const string Sessiondatapath = "C:\SessionData\";
private void Application_acquirerequeststate (object sender, EventArgs e)
{
System.IO.FileStream FS;
System.Runtime.Serialization.Formatters.Soap.SoapFormatter SF = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter ();
Try
{
Gets a specific cookie and exits if it is not found.
HttpCookie cookie = request.cookies["Permsessionid"];
if (cookie = null)
{
If not found, generates one (using pseudo-random sessionid)
cookie = new HttpCookie ("Permsessionid", Session.SessionID);
Make the cookie expire after 1 weeks
Cookie. Expires = DateTime.Now.AddDays (7);
Send it to the client browser
RESPONSE.COOKIES.ADD (cookie);
}
The file name equals the value of the cookie
String Permsessionid = cookie. Value;
Name of the generated data file
string filename = Sessiondatapath + permsessionid.tostring () + ". xml";
Open the file and, if an error occurs, exit
FS = new System.IO.FileStream (filename, IO. FileMode.Open);
Deserializes the Hashtable containing the value Hashtable HT = (Hashtable) sf. Deserialize (FS);
Move data to the session collection
Session.clear ();
foreach (String key in Ht. Keys)
{
Session (KEY) = HT (key);
}
}
Catch (Exception ex) {}
Finally
{
if (fs!= null) fs. Close ();
}
}
The code above implements the process of session persistence, and the code in the Aquirerequeststate event handler attempts to read a special client cookie named Permsessionid. The value of the cookie is treated as the name of an XML (on the server) that contains the value of the session variable that was saved at the end of the previous request, so the code populates the session collection before the page sees the new value. If the cookie does not already exist, the first request sent from the client is now visible. So the code creates a cookie and stores a unique string inside it. You should also create a server-side XML file in the ReleaseRequestState event and serialize all session variables into the XML file.