Introduction to ASP.NET Session
ASP.NET Session is simply a state from where we can retrieve the values of the user and stores it in the web page session. It is way in ASP.NET to ensure that the information which is passed from one page to another. Whenever the user makes interaction with any application on the website, firstly it enables the start of the new session and users to do many operations like reading and write on the websites. These values can be stored therein session object which will be stored on a server-side. Also for that particular user, it can use that value in any of the pages 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 that when does it exactly start. So, it will start 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. when a user hits any website or an application very first time, then and there itself session get started. If the user is not active or doesn’t do anything for a longer duration of time on the same page, the server places the session memory which is allocated for the user. Now, if again the user hits the page after session expiration then a new session is created again and that will be assigned for the user request. As it is server-side functionality, Session variables are not accessible by the web site user.
Let’s have a look at how session get started in below 4 ways:
1. When a user sends or requests for a URL which 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, which doesn’t have any valid SessionID cookie, automatically 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.
We can say that Session_Start fired when a new user visits or hits the application website.
When does the ASP.NET Session End?
We have seen that 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 webserver will collapse, the user session may get ended.
- By default, the value for the timeout is 20 minutes.
- If we want to reduce session timeout we can set it manually by setting its Session. Timeout Property.
- We can set its value according to our requirements of the web application. shorter session timeout will help to reduce the strain of our server’s memory resources.
- For example, if we know that the user will not stay more than a few minutes on the application’s page, then we must reduce the session timeout. Longer duration session timeout leads to open too many sessions which will affect the memory capacity of the server.
- Also, if we want to close or end the session purposely, we can use the method Session. Abandon of Session Object.
- Suppose 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 which will contain the Abandon command: <% Sesion.Abandon%>
- Likewise, we can set timeout property by setting 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 have any content or static object to contain.
So, in short, we can say Session is ended when Session_End Fired when a user ‘s session times out, ends or the user leaves that page of the application from the web site.
Examples to Implement ASP.NET Session
Let’s take an example of Session which will give us a better understanding. This example contains the creating a new session and storing its mail id.
This default.aspx code will allow the user to design its website or application by designing the user interface.
<%@ Page Title="Home Page"Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
<p>Please enter EmailID and Password</p>
<asp:TextBoxID="email" runat="server" TextMode="Email"></asp:TextBox>
<asp:TextBoxID="password" runat="server" TextMode="Password"></asp:TextBox>
<asp:ButtonID="login" runat="server" Text="Login" OnClick="login_Click" />
Default.aspx.cs will allows the developer to write their C# code to develop the website of an application.
public partial class _Default : Page
protected void login_Click(object sender, EventArgs e)
// 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 user logged in.
We can see in the above snippet that Email “Test.firstname.lastname@example.org” is stored in the session.
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 ended. With the help of an example, it is more clear what exactly the session is and how it deals in any web application.ASP.NET web application will by default create a session for data storage for users.
This is a guide to ASP.NET Session. Here we discuss how the session starts and end? and implementation of ASP.NET Session with examples. You can also go through our other related articles to learn more –