// Note, you need to reference 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.SOAP.SOAPFORMATTER ();
Try
{
// Get a specific cookie, if you can't find it, exit.
HTTPCOOKIE COOKIE = Request.Cookies ["permsessionID"];
IF (cookie == null)
{
// If you can't find it, generate one (use pseudo-random sessionID)
Cookie = New httpcookie ("PermsessionID", session.SessionID);
// Make the cookie due to the last week
Cookie.expires = datetime.now.adddays (7);
// send it to the client browser
Response.cookies.add (cookie);
}
/ / File name is equal to the value of this cookie
String permsessionID = cookie.value;
/ / Generate the name of the data file
String filename = sessionDataPath permsessionid.toString () ".xml";
// Open the file, if an error is wrong, exit
FS = new system.io.filestream (filename, IO.FILEMODE.OPEN);
// Reverse sequentially contains the value of HashTable HashTable HT = (Hashtable) sf.deSerialize (fs);
// Move the 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 above code implements the process of session persistence, and the code in the AquireRequestState event handler will try to read a special client cookie named PermsessionID. The value of the cookie is considered a name of an XML (on the server) that contains the value of the session variable saved at the end of the previous request, so the code will populate the session collection before the page sees the new value. If the cookie does not exist, it will now be seen that the first request from the client is now. So the code creates a cookie and stores the unique string in it. At the same time, you should also create an XML file for a server in the ReleaseRequestState event, sequence all session variables into the XML file.