Session State ASP.NET 4.0


Every ASP.NET application needs to manage the state management technique because of the state less nature of HTTP Protocol. In ASP.NET we have state management techniques like Application State, Session state and View state. Today we are going to discuss Session state management in ASP.NET and new features of ASP.NET 4.0 Session State. Are you looking for Session State ASP.NET 4.0 procedure? then find it here.

Learn about Session State ASP.NET 4.0


What is a Session State in ASP.NET?
Session state is state remembered for a particular user across all visits and all pages during their session in ASP.net.

Session State:


As we know, Session is one of the most important state management techniques in ASP.NET. You can enable or disable the Sessions in a page level or Application level in a Web.config file. By default Session state in enabled in ASP.NET and you can disable it whenever you want.

Below is the sample code to define the Session modes in ASP.NET web.config configuration file and ASP.NET page.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="WebApplication7.MyWebForm" EnableSessionState="True" %>


<sessionstate
mode="inproc"
cookieless="false"
timeout="80"
sqlconnectionstring="data source=PMDB;user id=useid;password=password" server="Your Server IP" port="80" /<

In ASP.NET session state supports several different storage options for session data to store the Sessions Object Data. Each option is identified by a value in the SessionState Mode enumeration. The following list describes the available session state modes.

InProc mode:


InProc mode stores session state data in memory on the Web server. This is the default mode in Session state and it is specified InProc SesstionStateMode enumeration Value. InProc mode stores session state values and variables in memory of the local web server. This is the only mode that supports Session_End Event. If we have big volume of data in sessions, the performance of the application may be degrading due to InProc mode. If you restart the server session data will be lost.

StateServer mode:


Which stores session state in a separate process called the ASP.NET state server. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. The stored session data will be available even the webserver is restarted. You need to encrypt the data while saving and retrieving the session info into state server. But as per the performance wise this will be little bit slow, because each session request needs to connect and fetch the data from the different server which we have saved the Session Data.

Below is the sample code to define the Session modes is StateServer in ASP.NET web.config configuration file.



<configuration>
<system.web>
<sessionState mode="StateServer"
stateConnectionString="tcpip=StateServerSample:8080"
cookieless="false"
timeout="80"/>
</system.web>
</configuration>

SQLServer mode:


SQLServer mode Stores session state data in a SQL Server database. This ensures that session state is preserved if the Web application is restarted and also makes session state available to multiple Web servers in a Web farm. You needs to encrypt the data and store into Database table.

Below is the sample code to define the Session modes is SQLServer in ASP.NET web.config configuration file



<configuration>
<system.web>
<sessionState mode="SQLServer"
sqlConnectionString="Integrated Security=SSPI;data
source=MYSqlServer;" />
</system.web>
</configuration>

Custom mode:


By using this mode, you can create your own session mode storage and store the session where ever you want

Off mode:


Off mode disables session state.
You can specify in your application which Session mode you want by setting the attribute "Mode" in Web.Config file. InProc and Off Modes does not require any additional information like server name, Ip address..etc, but other modes require.

Session State Behavior in ASP.NET 4.0


There was no feature to change the session state at runtime in code behind file till date in ASP.NET. But using ASP.NET 4.0, we can change the session state programmatically. The .NET 4.0 frameworks adds a new method SetSessionStateBehavior to the HttpContext class for ASP.NET. This method required SessionStatebehavior value to set the current session mode.
Below is the screen shot for session State Behavior in ASP.NET 4.0.
SessionStateBehaviour

While calling SetSessionStatebehavior, You can pass the following values as SessionStatebehavior :

Default:

If you set the SessionStatebehavior is default the changes won't be effect.

Disabled:

It will disable the Session State request.

ReadOnly:

it provides you the Read only access to your Session State.

Required:

your session state will get the both Read and Write Access.


Comments

Author: sridhar thota20 Aug 2015 Member Level: Gold   Points : 4

Different session modes in asp.net.

1.Inproc Session:
Session maintained in the website process is
called Inproc Session.
Restarting website will erase the session.

2.State server session:
Session maintained out side the website in the
state server is called state server session.
Restarting state server will erase the session.

3.Sql server session:
Session maintained out side the website in the
Sql server database is called sql server session.
Session is maintained until we delete from the
database table.

Regards

Sridhar Thota.
If you learn't from defeat..
You haven't really lost..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: