Follow by Email

Session in the asp.net

In this article i will explain how session works.

What is session?

A session is specific to the user and for each user a new session is created to track all the request from that user. Every user has a separate session and separate session variable is associated with that session. In case of web applications the default time-out value for session variable is 20 minutes, which can be changed as per the requirement,


Advatantages:

1.It helps to maintain the user states & data all over the application.

2. Any type of object can be stored in session.

3.It is secure.

Disadvantages:

If there are many user at a time on website then performance of the website may get lower as session data will be stored in server memory.


Using Session:

Following code is used to store the variable in session:

Session["variable"] = txtbox1.Text

e.g: Session[“username”] = txtUsername.Text

Like this we can retrieve variable value from the session

Textbox1.text = session[“variable”];


Session ID:

A session ID is an unique identification string usually a long, random and alpha-numeric string, that is transmitted between the client and the server. Session IDs are usually stored in the cookies, URLs (in case url rewriting) and hidden fields of Web pages.

Whenever client makes the request for the data , server looks at the SessionID & retrieves the corresponding data.


Session State:

It is the configuration of the web site for maintaining the session of the user.


Session modes:

It can be configured in web.config file of the website

There are 5 Session modes:

1. Off --> If session mode is off then session will be disabled for whole application.

InProc -->It is the default session mode of the asp.net. If we restart the server then allsession data will be lost in InProc mode.

StateServer --> It is the windows service which runs outside the application. This session state is totally managed by aspnet_state.exe.

In this session data is stored in the sql server.

Custom --> It gives full control to us to create every thing evean a session ID

Session Events:

Session_Start & Session_End are only two events available in asp.net.

Both the events can be handled using globle.asax file.This file has this code for both events.

void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
    }
    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
    }

0 comments:

Post a Comment