MSDN Documentation

Session State Overview

Session state enables you to store and retrieve user‑specific data across multiple requests within a web application. ASP.NET provides several session state modes to balance performance, scalability, and durability.

Getting Started

Enabling Session State

<configuration>
  <system.web>
    <sessionState mode="InProc" timeout="20" cookieless="UseCookies" />
  </system.web>
</configuration>

Storing Data

protected void Page_Load(object sender, EventArgs e)
{
    // Store a value
    Session["UserName"] = "Alice";

    // Retrieve a value
    string name = Session["UserName"] as string;
}

Removing Data

// Remove a single item
Session.Remove("UserName");

// Clear all items
Session.Clear();

// Abandon the session entirely
Session.Abandon();

Best Practices

Reference

Class / InterfaceDescription
HttpSessionStateProvides access to session values.
SessionStateModeEnumeration for session storage modes.
IHttpSessionStateInterface for custom session implementations.
SessionStateUtilityHelper methods for session state management.

On this page