How to use session state in asp.net website

By
Session state is used to store the information at the server end. Whenever we access the web application ,session id 128 ,256 or 512 bit generated.It depends which algorithm web are using . There are some algorithms which are used to encrypt and decrypt the session data.I will discuss this algorithm in coming next tutorials. Id is generated in the form of  non  persistent   cookie . We can used session object to store the user specific data.You have seen ,many website are compromised due to session attack.To improve the performance of websites,we  can use https,ssl and other protocol to secure your web applications.Each  client accessing a web application maintains a distinct session with the web server and there is also specific information associated with each of these sessions.Session state is defined in the <session state>element of the web.config file.It also stores the data specific to a user session in session variables . Different  session variables are created for each user session.Session Id is transferred between the server and the client over to HTTP protocol using cookies.
Example:-When we open(login) your gmail account on your web browser then we can open all google account(orkut,google+,you tube,blogger,google analytic,adsense etc.) through session id.Because i have told that session id is transferred between the server and the client over to HTTP Protocol using cookies.
There are some steps to implement the session state variables on the as.net web applications.
Step 1:-First open your visual studio -->File-->New-->website-->select ASP.NET Empty website -->OK-->open solution explorer -->Add New Web Form -->drag and drop label,Text box and Button control on the form as shown below:-

session_form

Step 2:- Double click on Submit button -->write the following codes which are given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class session : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["id"] = TextBox1.Text;
        //Session["pass"] = TextBox2.Text;
        Response.Redirect("default.aspx");
        Session.RemoveAll();
    }
}

Step 3:-  No Add a New web Form -->drag and drop label and button control on te web Form as shown below:-
logout

Step 4:- Double click on Logout button-->Write the following codes which are given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["id"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Session.Remove("id"); you wana remove one by one use this.
        Session.RemoveAll();
        Response.Redirect("session.aspx");
    }
}


Step 5:- Now Run the application (press F5)-->Fill the required field details as shown below:-
run_session
Step 6:-  Now click Submit button --> you will see following output-->now click Logout button--> you will redirect to home page(destroy session).


output
For More...
  1. Query string is asp.net website
  2. oops concepts in c#
  3. web form control
  4. validation controls
  5. create captcha image for asp.net application
  6. Send mails from asp.net application free
  7. File handling in c#
  8. Reflection in c#
For whole session application
            Download

2 comments:

Powered by Blogger.