Saturday, 26 May 2018

Cookies


Cookies

It is small amount of info stored by web server on client machine.

Types of Cookies :

  • Temperary Cookie (the info will be lost when user closes the browser or logout).
  • Permanent Cookie(persistent cookie) : the info will be stored on client machine till the time specified).

For Example

UserName
Password
protected void Button1_Click(object sender, EventArgs e)
{
     ///////////////////////// temporary cookie creation
    HttpCookie cook = new HttpCookie("vilas");
    DateTime dt = DateTime.Now;
    cook.Values.Add("currentdatetime", dt.ToString());
    cook.Values.Add("username", TextBox1.Text);
    cook.Values.Add("password", TextBox2.Text);
    Response.Cookies.Add(cook);
    Label3.Text = "temp cookie created";
}
protected void Button2_Click(object sender, EventArgs e)
{
          ///////////////////////// permanent cookie creation
          HttpCookie cook = new HttpCookie("conco");
          DateTime dt = DateTime.Now;
          cook.Values.Add("currentdatetime", dt.ToString());
          cook.Values.Add("username", TextBox1.Text);
          cook.Values.Add("password", TextBox2.Text);

          ///////////////////////// for permanant
          cook.Expires = dt.AddMinutes(10);
          // DateTime dt1=new DateTime(10,31,2014);
         // cook.Expires = dt1;
          Response.Cookies.Add(cook);
          Label3.Text = "perm cookie created";
}
protected void Button3_Click(object sender, EventArgs e)
{
          ///////////////////////// reading cookie
          HttpCookie cook = Request.Cookies["conco"];
          if (cook != null)
          {
          string dat, user, pass;
          dat = cook.Values["currentdatetime"].ToString();
          user = cook.Values["username"].ToString();
          pass = cook.Values["password"].ToString();
          Label3.Text="date time of creation of cookie is " + d + "--"+"user name is " + u + "--"+"password is " + p + "--";
          }
          else
          {
          Label3.Text = "no cookie present";
          }
}

0 comments:

Post a Comment

Introduction to C# .NET

It  is  pure  object  oriented  programming  language.  Also  it  is  Event Driven Programming Language. It is case sensitive. ...