How to implement cookie in ASP.NET Websites

By
Cookie is a small text file which can be used to store the user specific information for personalised the web pages. It stores the data at the user end not server side.When a browser requests a web page again,the cookie is sent along with the request.The web server then retrieves the information from the cookie.In this tutorial i will implement two types of cookie which are given below:-
     1. ) Non Persistent cookie(Temporary cookie) :-
It is known as Temporary cookie.This type of cookie resides within browser memory,So it is called session cookie also.
There are some steps to implement the Non persistent cookie in ASP.NET Website which are given below:-
Step 1:- First open your visual studio--> File-->New-->Website-->ASP.NET Empty website-->OK -->open solution Explorer-->Add a New web forms --> drag and drop label,Text Box,check box,SQL Datasource and Button control on the form as shown below:-


web form

Step 2:- Now Add a Database.mdf file --> make student table --> Add some values Id and Password as shown below:-

database.mdf

Note:- If you face any problem visit below link:-
  1. Add .mdf Database  on ASP.NET Website
  2. Solve sql server problems
Step 3:-  Now Double click on Login Button and Write the following codes:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        String str = "select count(*) from student where id='" + TextBox1.Text + "'and pass='" + TextBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(str, con);
        int obj = Convert.ToInt32(cmd.ExecuteScalar());
        if (obj > 0)
        {
            if (CheckBox1.Checked)
            {
                HttpCookie mycookie = new HttpCookie(TextBox1.Text, TextBox2.Text);
                Response.Cookies.Add(mycookie);
            }

            Response.Redirect("default.aspx");
        }
        else
            Label4.Text = "invalid username or password";
        con.Close();
    }
}

Step 4:- Now Run the Application-->Filed the required field and check the Remember Me Box as shown below:-


home

Step 5:-Now press login Button--.you can find the browser cookie data by clicking on certificate as shown below:-
browser _cookie

Step 6:- Now Run again your application -->you will see that id and password  field  automatically filled through the cookie data as shown below:-


relogin

Note:- This type of cookie data resides in the browser memory. when we close the browser then data is lost.
     2. ) Persistent Cookie:- 
This type of cookie resides within  client hard disk till the time ,it Expiry time is over.It stores the data in the user hard disk. 
There are some steps to implement the persistent cookie on the ASP.NET website.which are given below:-
Step 1:- First open your visual studio--> File-->New-->Website-->ASP.NET Empty website-->OK -->open solution Explorer-->Add a New web forms --> drag and drop label,Text Box,check box,sql Datasource and Button control on the form as shown below:-


persistent_home

Step 2:- Now Double click on Login Button and write the following  codes which are given below:-

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class persistantcookies : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        String str = "select count(*) from student where id='" + TextBox1.Text + "'and pass='" + TextBox2.Text + "'";
        SqlCommand cmd = new SqlCommand(str, con);
        int obj = Convert.ToInt32(cmd.ExecuteScalar());
        if (obj > 0)
        {
            if (CheckBox1.Checked)
            {
                HttpCookie mycookie = new HttpCookie(TextBox1.Text, TextBox2.Text);
                mycookie.Expires = DateTime.Now.AddDays(5);
                Response.Cookies.Add(mycookie);
            }

            Response.Redirect("default.aspx");
        }
        else
            Label4.Text = "invalid username or password";
        con.Close();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("tempcookies.aspx");
    }
}


Step 3:- Now Run the Application --> filled the required field  details -->check the Remember Me box as shown below:-


fill_form

Step 4:- Now press Login Button.see  output:-



 Note:- This type of cookie store the id and password Information in a text file in user hard disk.
Step 5:-  To view the cookie data in computer hard disk visit following links.
For Internet Explorer:-
Windows 7 and Windows Vista has 2 cookies folders:-
  1. C:\Users\Your User Name\AppData\Roaming\Microsoft\Windows\Cookies
  2. C:\Users\Your User Name\AppData\Roaming\Microsoft\Windows\Cookies\Low
For chrome Browser:-

  1. C:\Users\Your User Name\AppData\Local\Google\Chrome\User Data\Default\Local Storage
Now i am going to show you cookie data(text file)for our application.You can enter above link in any Browser or window explorer ,you can easily find cookie data(text file).
I have enter above link in window Explorer:-
C:\Users\Ramashanker\AppData\Roaming\Microsoft\Windows\Cookies
persistent_cookie

Note:- Most of the attack found on your cookie data that resides in your hard disk memory  . You will have to seen many account easily  compromised through client's cookie data.
.Every Browser store cookies data in different different location in your system.In persistent cookie i have used Interner Explore Browser.
For More:-
  1. File handling in c#
  2. Constructor and destructor
  3. web form controls
  4. Multithreading in c#
  5. Main elements are used for compiling the c# code
  6. Collections in c#
  7. Method overloading
To Get the Latest  Free Updates Subscribe
Click below for download whole application
            Download

0 comments:

Post a Comment

Powered by Blogger.