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 !!