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:-
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:-
Step 2:- Now Add a Database.mdf file --> make student table --> Add some values Id and Password as shown below:-
Note:- If you face any problem visit below link:-
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 5:-Now press login Button--.you can find the browser cookie data by clicking on certificate as shown below:-
Step 6:- Now Run again your application -->you will see that id and password field automatically filled through the cookie data as shown below:-
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:-
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:-
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.
For Internet Explorer:-
Windows 7 and Windows Vista has 2 cookies folders:-
- C:\Users\Your User Name\AppData\Roaming\Microsoft\Windows\Cookies
- C:\Users\Your User Name\AppData\Roaming\Microsoft\Windows\Cookies\Low
- C:\Users\Your User Name\AppData\Local\Google\Chrome\User Data\Default\Local Storage
I have enter above link in window Explorer:-
C:\Users\Ramashanker\AppData\Roaming\Microsoft\Windows\Cookies
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:-
- File handling in c#
- Constructor and destructor
- web form controls
- Multithreading in c#
- Main elements are used for compiling the c# code
- Collections in c#
- Method overloading
Click below for download whole application
Download
0 comments:
Post a Comment