Updated June 27, 2023
Introduction to ASP.NET Session
ASP.NET Session is simply a state from which we can retrieve the user’s values and store them in the web page session. It is the way in ASP.NET to ensure that the information is passed from one page to another. Whenever the user interacts with any application on the website, firstly, it enables the start of the new session and users to do many operations like reading and writing on the websites. These values can be stored in the session object, which will be stored on the server side. Also, for that particular user, it can use that value on any page on the same website. So, every value has a unique ID.
When does the ASP.NET Session Start?
We all must have a basic question when does it exactly start? So, it will begin when we hit the website. The ASP.NET session will be generated as soon as the first request has been made to the web application. By default, the session state is in Process. The session starts when a user hits any website or application for the first time. If the user is inactive or doesn’t do anything longer on the same page, the server places the session memory allocated for the user. Now, if the user hits the page after the session expiration again, a new session will be created and assigned for the user request. As it is server-side functionality, Session variables are not accessible by the website user.
Let’s have a look at how ASP.NET Session gets started in below four ways:
1. When a user sends or requests for a URL that identifies an ASP.NET File in that application. Session_OnStart event procedure is included in Global.asa File for that application.
2. The session object is used to store the user values.
3. Whenever the server receives a request with no valid SessionID cookie, a new session gets started.
4. <OBJECT> tag is used by the Global.asa file to initiate an object with its session scope. Also, in an application, the user will request a .asp file.
When a new user visits or hits the application website, we can say that Session_Start is fired.
When does the ASP.NET Session End?
We have seen when exactly the ASP.NET session starts; let’s see when exactly it ends.
- Many of us experience this scenario of session end. When we are on a web page of an application for a longer time, and we have not requested or refreshed that page for a longer duration, then the session will end automatically.
- When the web server collapses, the user session may get ended.
- By default, the value for the timeout is 20 minutes.
- We can manually set it as a session if we want to reduce session timeout. Timeout Property.
- We can set its value according to our requirements for the web application. A shorter session timeout will help to reduce the strain on our server’s memory resources.
- For example, if the user does not stay more than a few minutes on the application’s page, we must reduce the session timeout. Longer duration session timeout leads to opening too many sessions, which will affect the server’s memory capacity.
- Also, if we want to close or end the session purposely, we can use the method Session. Abandon of Session Object.
- If we want to quit the web page, then we can add a quit button on our web page of an application with the ACTION parameter set to the URL of a .asp file containing the Abandon command: <% Sesion.Abandon%>
- Likewise, we can set the timeout property by setting the command as a <% Session.Timeout =5 %> in .asp file.
- Sessions that do not have any state will automatically end. Due to the stateless session, its session object doesn’t contain any content or static object.
The session ends when the application’s website fires the Session_End event due to a user’s session timeout, termination, or departure from the page.
Examples to Implement ASP.NET Session
Let’s take an example of a Session that will give us a better understanding. This example contains creating a new session and storing its mail id.
1. Default.aspx
This default.aspx code will allow the user to design its website or application by designing the user interface.
Code:
<%@ Page Title="Home Page"Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="SessionExample._Default"%>
<head>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 105px;
}
</style>
</head>
<formid="form1"runat="server">
<p>Please enter EmailID and Password</p>
<tableclass="auto-style1">
<tr>
<tdclass="auto-style2">Email</td>
<td>
<asp:TextBoxID="email" runat="server" TextMode="Email"></asp:TextBox>
</td>
</tr>
<tr>
<tdclass="auto-style2">Password</td>
<td>
<asp:TextBoxID="password" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<tdclass="auto-style2"> </td>
<td>
<asp:ButtonID="login" runat="server" Text="Login" OnClick="login_Click" />
</td>
</tr>
</table>
<br/>
<asp:LabelID="Label3" runat="server"></asp:Label>
<br/>
<asp:LabelID="Label4" runat="server"></asp:Label>
</form>
2. Default.aspx.cs
Default.aspx.cs will allow the developer to write their C# code to develop the website of an application.
Code:
usingSystem;
usingWeb.UI;
namespaceSessionExample
{
public partial class _Default : Page
{
protected void login_Click(object sender, EventArgs e)
{
if (password.Text=="edu123")
{
// Storing email to Session variable
Session["email"] = email.Text;
}
//check for session variable which should not be empty
if(Session["email"] != null)
{
// I will display the stored email
Text = "Email is stored to the session.";
Text = Session["email"].ToString();
}
}
}
}
Let’s see the output for the above example code. It will email the id of a user to the session when the user logs in.
Output:
In the above snippet, it is evident that the session stores the email “[email protected].”
Conclusion
We have learned about ASP.NET sessions in this article. I hope now it is very clear about when the ASP.NET Session gets started and ends. With the help of an example, it is clear what exactly the session is and how it deals with any web application. ASP.NET web application will, by default, create a session for data storage for users.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET Session” was beneficial to you. You can view EDUCBA’s recommended articles for more information.