---
title: "Session in C#"  
description: "When we are working with an application on your computer, you open it, do some changes and then you close it. This is much like a Session."  
author: "Tom Cruser"  
published: 2016-05-06  
updated: 2018-03-27  
canonical: https://www.mindstick.com/articles/12042/session-in-c-sharp  
category: "c#"  
tags: ["c#", "session"]  
reading_time: 2 minutes  

---

# Session in C#

When we are working with an [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) on [your computer](https://www.mindstick.com/articles/13023/choosing-the-best-power-cord-for-your-computer-a-layman-s-guide), you open it, do some changes and then we close it. This is much like a Session. The computer knows who you are. It knows when we open the application and when we close it. However, on the internet there is one problem: the [web server](https://www.mindstick.com/articles/23199/digital-personal-server-the-premium-web-server) does not know who we are and what we do, because the HTTP address doesn't maintain state.

ASP solves this problem by creating a unique cookie for each user. The cookie is sent to the user's computer and it contains information that identifies the user. This [interface](https://www.mindstick.com/articles/12036/interface-in-c-sharp) is called the [Session object](https://www.mindstick.com/forum/2365/session-object-in-mvc).

The Session [object stores](https://www.mindstick.com/interview/34321/can-you-separate-concerns-using-multiple-object-stores-efficiently-when-would-you-avoid-that) information about, or change settings for a user session.

[Variables](https://www.mindstick.com/articles/715/php-variables) stored in Session object hold information about one single user, and are available to all pages in one application. Common [information stored](https://answers.mindstick.com/qa/51523/does-the-iphone-support-filevault-is-information-stored-on-the-iphone-encrypted-and-password-protected-does-it-have-biometric-capabilities) in [session variables](https://www.mindstick.com/forum/267/session-variables) is name, id, and preferences. The server creates a new Session object for each new user, and destroys the Session object when the session expires.

**Example:** below provided a complete [source code](https://www.mindstick.com/forum/23271/is-there-a-way-to-get-the-source-code-from-an-apk-file) on Session.

**LoginSession.aspx file**\

```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginSession.aspx.cs" Inherits="LoginSession" %>   <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head id="Head1" runat="server">    <title></title>    <link href="StyleSheet.css" rel="stylesheet" type="text/css" /></head><body>    <form id="form1" runat="server">        <div align="center" style="background-color: azure">            <h2 style="background-color: chocolate; font-family: Cambria; text-align: center">Login Page <hr /> </h2>            <table border="0">                <tr>                    <td>Name: </td>                    <td>                        <asp:TextBox ID="txtUserName" runat="server" />                    </td>                </tr>                 <tr>                    <td></td>                </tr>                <tr>                     <td></td>                </tr>                 <tr>                     <td>                        <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" CssClass="btn" Width="100px" ForeColor="Tomato" BorderStyle="Ridge" />                    </td>                    <td></td>                </tr>             </table>            <hr />            <asp:Label ID="lblstatus" runat="server" />         </div>    </form></body></html>
```

**LoginSession.aspx.cs file**\

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class LoginSession : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {     }    protected void btnLogin_Click(object sender, EventArgs e)    {        // Create new session variable named "username"         Session["username"] = txtUserName.Text;         Response.Redirect("SessionStart.aspx");      }}
```

## \

**SessionStart.aspx file**\

```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SessionStart.aspx.cs" Inherits="SessionStart" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:label ID="lbl1" runat="server"/>                      </div>    </form></body></html>
```

## \

**SessionStart.aspx.cs file**\

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls; public partial class SessionStart : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {         if (Session["username"] != null)        {            lbl1.Text = "Welcome " + Session["username"].ToString();        }     }}
```

---

Original Source: https://www.mindstick.com/articles/12042/session-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
