How to work with Sessions in ASP.Net

Accept advantage of sessions to shop and manage data pertaining to a logged user's session

ASP.NET Session

HTTP is a stateless protocol. This implies that every time a new request is sent from the client to the server the land information of the previous request is lost. In that location are several means to shop and manage state in ASP.Internet. Session object is one of them, the others being Caching and Application objects.

Caching improves the application's functioning past minimizing the consumption of resources in your arrangement. You can store frequently used data or Spider web pages to improve the application'south performance and throughput and scalability by reducing the consumption of the server'due south resources.

You can define session as a session of connectivity between the server and the client -- the session object holds data that correspond to a user'southward session. Session is a server side state management technique that is used to store user specific information in the memory for later retrieval.

Modes of storing session information

The session object is created and managed at the server side. Session storage fashion determines where your session data should be stored. Session state can be stored in one of the following modes:

  1. In - Process: Stored in the same ASP.Internet Process
  2. State Server: Stored in the some other system
  3. SQL Server: Stored in the SQLServer database
  4. Custom: this enables you to shop session information using a custom storage provider

The In-Proc mode of storage of session data is the default mode and information technology is also the fastest of all the available storage modes. In this mode, the session information is stored in the server's memory -- inside the ASP.Internet worker process. You should use this mode if the amount of data that needs to be stored in the session is less and if y'all wouldn't need the information to exist persisted. Information technology should be noted that session data stored in this fashion is volatile, i.eastward., every bit soon equally the session is terminated the session data is lost. And so, data in the session is available every bit long every bit the session is alive.

In the State Server style, the session data is stored in a separate process - this is called the ASP.Net State Service. In other words, session data in this mode is stored exterior of the ASP.Net worker process or the application pool in IIS. Unlike the In-Proc mode, session data in the State Server manner is preserved, i.due east., it's not lost after your web awarding is restarted.

The post-obit code snippet illustrates how you tin configure session state in your application to exist stored in this mode.

<configuration>

  <system.web>

    <sessionState mode="StateServer"

      stateConnectionString="tcpip=IDGServer:1234"

      cookieless="false"

      timeout="20"/>

  </system.web>

</configuration>

The SQLServer mode of session data storage is used to persist session data of your application in the SQLServer database. Similar to the State Server manner of session data storage, the SQLServer mode also enables you to persist session data of your application across application restarts. Annotation that y'all should ensure that the ASP.Cyberspace session state database is created before using this mode. You can create this database using the Aspnet_regsql.exe control line utility.

The following code snippet shows how you can configure your application to store session information in a SQLServer database.

<configuration>

  <organization.web>

    <sessionState style="SQLServer"

       sqlConnectionString="information source=server;user id=joydip;password=sa1@3"

       cookieless="simulated" timeout="xx" />

  </organization.web>

</configuration>

Support for Session information pinch

Microsoft's ASP.Internet 4 introduced a new feature: session state pinch. With ASP.Net 4 and onward, you tin leverage this built-in feature to shrink session information for storing out-of-process sessions. To take reward of this characteristic, all you lot need to exercise is set the compressionEnabled aspect to "true" in the configuration file of your application. The following code snippet illustrates how this can exist configured.

<sessionState

  mode="SQLServer"

  stateConnectionString="some connectedness cord..."

  compressionEnabled="true"/>

Session state enables you lot to shop user specific information in the memory and identify a particular request uniquely.  Session information is stored every bit central/value pairs in the SessionStateItemCollection and can be accessed using the HttpContext.Session property.

The post-obit code examples show how y'all tin can store and retrieve session data.

HttpSessionState.Session["UserName"] = "John"; //stores session information

string str = HttpSessionState.Session["UserName"].ToString();

// Retrieves session information

HttpSessionState.Remove("Key to remove");

//Removes an object from the session country

Copyright © 2016 IDG Communications, Inc.