Powered by Blogger.

Thursday, March 13, 2014

Session management in Asp.Net

In this article we will discuss about session state state management technique.

Also you can check out:

- Tutorials on WCF in Asp.Net

- Get all time zones in C#.Net

- How to place C#.Net windows application to System Tray?

Through cookies or query string it is very difficult to store and access complex information like custom data objects. Session allows you to store any type of data in memory on the server. The information is protected, because it is never transmitted to the client. Every client that accesses the application has a different session.

Most of the eCommerce site uses session mechanism in asp.net.

ASP.NET tracks each session using a unique 120-bit identifier. That is known as sessionid which is transmitted between the web server and the client.

Below is the syntax to store value in Session in Asp.Net

Session["UserName"] = "Your User Name";

Below is the way to retrieve value from the session in Asp.Net.

if (Session["UserID"] != null)
string userName = Session["UserID"].ToString();

The session id is submitted through a cookie name as ASP.NET_SessionId. Or a Session ID is transmitted in a specially modified URL. This is used in case where the client does not support cookies.

- Session state is global to your entire application for the current user.

- It stores in the server memory, so it might be create a performance issue.

But there are certain situation where Session could be lost:

- If the user closes and restarts the browser.

- If the user accesses the same page through a different browser window, although the session will still exist if a web page is accessed through the original browser window. Browsers differ on how they handle this situation.

- If session time out occurs.

- If you call the method Session.Abandon() programmatically in any web page of the site.

- If restart IIS.

Below is an example that stores a dataset in the session: