Table of contents
1.
Introduction
2.
Working Example
3.
ASP.NET Session Events
4.
ASP.NET Session Mode
4.1.
InProc
4.2.
StateServer
4.3.
SQL Server
4.4.
Custom
5.
Frequently Asked Questions
5.1.
What is ASP.NET?
5.2.
What is ASP.NET in C#?
5.3.
What is the difference between ASP.NET and C#?
5.4.
What is an ASP.NET session?
5.5.
What is Cookieless in web config?
6.
Conclusion
Last Updated: Jun 11, 2024
Medium

ASP.NET Session

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ASP.NET session state lets you store and retrieve a user's values as the user navigates ASP.NET pages in a Web application for the particular time session. HTTP is a stateless protocol which means that a Web server treats every HTTP request for a page as an independent request. The server maintains no knowledge of variable values used during prior requests. ASP.NET session state determines requests from the same browser during a session and provides a way to store variable values for the duration of that session. By default, all the ASP.NET applications enable the ASP.NET session state.

Alternatives to ASP.NET session state contain the following:

  • Application state stores variables that all users of an ASP.NET application can access.
  • Profile properties persist user values in a data store without expiring them.
  • Cookies.
  • View state, which continues values on a page.
  • The fields and query string on an HTML form is open from an HTTP request.
  • ASP.NET caching stores values in memory that are available to all ASP.NET applications.

Note: Each of the ASP.NET sessions created is stored in the SessionStateItemCollection object.

Working Example

Here's an example of an input form that stores the user's email address.

// ASPnetSessionExample.aspx

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="ASPnetSessionExample.aspx.cs"  
Inherits="codingNinjas.ASPnetSessionExample" %>  
<head>  
    <style type="text/css">  
        .table-style {  
            width: 100%;  
        }  
        .table-data-style {  
            width: 105px;  
        }  
    </style>  
</head>  
<form id="form1" runat="server">  
    <p>Please fill the following details</p>  
    <table class="table-style">  
        <tr>  
            <td class="table-data-style">Email:</td>  
            <td>  
                <asp:TextBox ID="email" runat="server" TextMode="Email"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td class="table-data-style">Password:</td>  
            <td>  
                <asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox>  
            </td>  
        </tr>  
        <tr>  
            <td class="table-data-style"> </td>  
            <td>  
                <asp:Button ID="login" runat="server" Text="Login" OnClick="loginBtn" />  
            </td>  
        </tr>  
    </table>  
    <br />  
    <asp:Label ID="UpperEmail" runat="server"></asp:Label>  
    <br />  
    <asp:Label ID="Email" runat="server"></asp:Label>  
</form>

 

// ASPnetSessionExample.aspx.cs

using System;
using System.Web.UI;  
namespace codingNinjas  
{  
    public partial class ASPnetSessionExample : Page  
    {  
        protected void loginBtn(object sender, EventArgs e)  
        {  
            if (password.Text=="qwerty"){
                // Store email into session variable  
                Session["email"] = email.Text;  
            }  


            if (Session["email"] != null){  
                // Display email
                UpperEmail.Text = "The email stored to the session.";  
                Email.Text = Session["email"].ToString();  
            }  
        }  
    }  
}


Output:

  Idle:

  On Submission:

ASP.NET Session Events

There are two types of events available in the ASP.NET Session. We can handle both sessions in a global.asax file.

  • Session_Start(): The session_start event raises when initializing a new session.
  • Session_end(): The Session_End event raises when the session is Expires.

ASP.NET Session Mode

We can divide ASP.NET Session Mode into the following four types:

InProc

The InProc is the default Session Mode. The Session Mode is kept in the application worker process (aspnet_wp.exe) in the application domain using this Session Mode. The Worker Process depends on the IIS server version. The ASP.NET worker thread handled the memory location. It only involves a substantial overhead for the worker thread to manage these. Also, since this is in the server's memory, the chances are that extensive session information would lead to more memory usage and hence lower the performance.

StateServer

StateServer is also the Out-Proc Session mode. StateServer uses a stand-alone Windows Service independent of IIS and can run on a separate server. We manage this session state by aspnet_state.exe. And the Session Variables are stored in ASP.NET State service.

SQL Server

We store the session data inside the SQL Server database in this mode, so we need to follow these steps to keep the session in the database.

Custom

In custom session mode, we can control everything, like session id, and all it means is that you can create your algorithm to create a session id. It uses less something compared to others. You can create your session state provider, for example, Oracle.

Frequently Asked Questions

What is ASP.NET?

ASP.NET is an open-source, server-side web development framework designed to produce dynamic web pages. Microsoft developed it to allow programmers to build active websites, applications, and services.

What is ASP.NET in C#?

ASP.NET is a web-based application framework developed and marketed by Microsoft to let programmers build dynamic websites. It will enable you to use a full-featured programming language such as C# or VB.NET to build web applications quickly.

What is the difference between ASP.NET and C#?

The difference between ASP.NET and C# is that ASP.NET is an open-source framework for web application development to create dynamic content over web pages. C# is an object-oriented, functional, generic, imperative, and component-based programming language.

What is an ASP.NET session?

ASP.NET session state lets you store and retrieve a user's values as the user navigates ASP.NET pages in a Web application for the particular time session. HTTP is a stateless protocol which means that a Web server treats every HTTP request for a page as an independent request.

What is Cookieless in web config?

By default, the identifier for a session is in a non-expiring session cookie in the browser. We can specify not to store session identifiers in a cookie by setting the cookieless attribute to true in the sessionState configuration element.

Conclusion

This article teaches about ASP.NET Session and how we use them. We saw why ASP.NET Session could be beneficial for a developer to learn. 

Click here to read about the Top 10 web development frameworks. Apart from this, you can also expand your knowledge by referring to these articles on Features Of ASP Net and ASP Full Form. Check out our Web development course and blogs on Backend Web Technologies.

If you are preparing for your DSA interviews then, Coding Ninjas Studio is a one-stop destination. This platform will help you acquire effective coding techniques and overview student interview experience in various product-based companies.

Happy Learning!

Live masterclass