What is Session? Simply put, it is the number that the server sends to the client. When a WWW server is running, several users may browse the website running on this server. When a user establishes a connection with the WWW server for the first time, the user establishes a Session with the server, and the server automatically assigns a SessionID to the user to identify the unique identity. This SessionID is a random string consisting of 24 characters on the WWW server. We will see it in the following experiment.
This unique SessionID has great practical significance. When a user submits a form, the browser automatically attaches the user's SessionID to the HTTP header information (this is an automatic function of the browser and the user will not notice it ), after the server processes this form, it returns the result to the user corresponding to SessionID.
If a session ends
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. asp tutorial x. cs" Inherits = "UsingSessionEnd" EnableSessionState = "ReadOnly" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> Using Session State-End </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "container">
<H1> Using Session State-End Session value:
<Asp: Label ID = "labSession" runat = "server"/> <br/>
<Asp: HyperLink ID = "lnkSession" runat = "server" NavigateUrl = "UsingSessionStart. aspx" Text = "Go to session start page"/> <br/>
</Div>
</Form>
</Body>
</Html>
File: Default. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class UsingSessionEnd: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (Session ["Name"] = null)
LabSession. Text = "Session has timed out or not been initialized ";
Else
{
String name = (string) Session ["Name"];
LabSession. Text = name;
}
}
}
Use Cookieless session status
The web configuration file enables cookieless sessions by assigning the value AutoDetect to the cookieless attribute.
File: Web. Config
<Configuration>
<System. web>
<SessionState
Cookieless = "AutoDetect"
RegenerateExpiredSessionId = "true"/>
</System. web>
</Configuration>
Session Timeout
File: Web. Config
<Configuration>
<System. web>
<SessionState timeout = "60"/>
</System. web>
</Configuration>
You can modify the Session timeout value programmatically with the Timeout property of the Session object.
Session. Timeout = 60;