Thursday 25 July 2013

Difference between cookie and session? || what is Cookie? || What is Session? in .NET || Cookies in ASP.NET

Hi Friends, in this article I will explain about what is Cookie? What is Session and Difference between cookie and session?
What is cookie?
  • A cookie is often used to identify a user.
  • Cookies have so many names likes HTTP Cookie, Web Cookie, Browser Cookie, Session Cookie, etc.
  • A cookie is a small file that the server embeds on the user's computer.
  • Each time the same computer requests a page with a browser, it will send the cookie too.
  • With ASP, you can both create and retrieve cookie values.
I explain the cookie with one small live example.
  • Some sites store your username and password 7 days or 10 days .Actually they store your details as cookie. Cookie is stored in User’s computer.
  • If you observe or not in the Facebook , Gmail etc. sites ,when you are log in it have a option like Keep me signed in if you click on the check box it will store the username as cookie in your system. If you open that site in your system it automatically enter your username. If you open that site in your friend system it automatically enter your friend username if he follow above procedure otherwise it will shows empty box.
How to Create a Cookie?
We can write cookie in two ways
Way 1:
In VB.NET:  
 Response.Cookies("userName").Value = "Aspdotnet-Roja"
   Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
In C#:
Response.Cookies["userName"].Value = "Aspdotnet-Roja";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

Way 2:
In VB.NET:
Dim Cookie As New HttpCookie("username")
Cookie.Value = DateTime.Now.ToString
Cookie.Expires = DateTime.Now.AddDays(1)
Response.Cookies.Add(Cookie)

In C#:
HttpCookie Cookie = new HttpCookie("username");
Cookie.Value = DateTime.Now.ToString();
Cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(Cookie);

Example:
  • Create one website
  • Take one label one textbox.
  • Write the following code:
In VB.NET:


Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Cookies("userName").Value = "Aspdotnet-Roja"
        Response.Cookies("userName").Expires = DateTime.Now.AddDays(1)
        uname.Text = Request.Cookies("userName").Value
    End Sub
End Class

In C#:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class _Default : System.Web.UI.Page
{
      protected void Page_Load(object sender, System.EventArgs e)
      {
            Response.Cookies["userName"].Value = "Aspdotnet-Roja";
            Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);
            uname.Text = Request.Cookies["userName"].Value;
      }
    Public _Default()
      {
            Load += Page_Load;
      }
}

When we run the above it will show like below figure.


If you want to check the cookie Run the above code and comment the Response.Cookies("userName").Value = "Aspdotnet-Roja"
Again run the code textbox shows Aspdotnet-Roja because cookie is stored to one day.If you the same page tomorrow it will deleted.

How to delete the cookie?
 In VB.NET:

If Request.Cookies("userName ") IsNot Nothing Then
   Response.Cookies("userName ").Expires = DateTime.Now.AddDays(-1)
            'to refresh the page
   Response.Redirect("Index.aspx")
End If

In C#:

if (Request.Cookies("userName ") != null) {
      Response.Cookies("userName ").Expires = DateTime.Now.AddDays(-1);
      //to refresh the page
      Response.Redirect("Index.aspx");
}


I will write what is session? And difference between cookie and session in further articles.

I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.