Implement session persistence (permanent sessions)

Source: Internet
Author: User
Tags implement soap key serialization sessions client
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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.