Simulate Session principles

Source: Internet
Author: User

Session is a small container for user personal information stored on the server! Used to save key information for each user! Each user creates a SessionID during access, which is saved to the cookie of the browser. The server associates users in the browser according to the SessionID.

Create a SessionManager class to manage sessions. :

Manage Session classes

Public class SessinManager
{// Simulate the asp.net session principle!
Private static IDictionary <string, IDictionary <string, object> data = new Dictionary <string, IDictionary <string, object> ();
Public static IDictionary <string, object> GetSession (string sessionID)
{
If (data. ContainsKey (sessionID) // This id is closely related to the browser. Basically, a browser has a SessionID.
{// Is generally stored in the cookie of the browser.
Return data [sessionID];
}
Else
{
IDictionary <string, object> session = new Dictionary <string, object> ();
Data [sessionID] = session; // create a Dictionary with the passed SessionID.
Return session;
}
}
}

SessionID is generated during user access to create the Session space.

Set and read Session

Protected void Page_Load (object sender, EventArgs e)
{
If (Request. Cookies ["MySessionID"] = null) // set the SessionID on the website homepage (portal.
{
String sessionID = Guid. NewGuid (). ToString (); // a random ID is generated and saved to the cookie.
Response. SetCookie (new HttpCookie ("MySessionID", sessionID ));
}
}
Protected void SetSession_Click (object sender, EventArgs e)
{
String sessionID = Request. Cookies ["MySessionID"]. Value; // read SessionID from cookie
IDictionary <string, object> session = SessinManager. GetSession (sessionID );
// Access the memory space opened by the server based on the SessionID stored in the cookie.
Session ["UserMessage"] = ""; // set the value in the Session
}
Protected void GetSession_Click (object sender, EventArgs e)
{
String sessionID = Request. Cookies ["MySessionID"]. Value;
IDictionary <string, object> session = SessinManager. GetSession (sessionID );
Button2.Text = session ["UserMessage"]. ToString (); // read the value in the Session.
}

Thank you for your advice. Thank you !!

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.