Discuss Cookie in ASP.NET.

Cookie is the small text file which is stored on client machine. It uses to store the small amount of information on client.
Cookie consists of the following two parts:
Key – Name of Cookie.
Value – Information stored in the cookie.
We also can specify the life of a cookie, if you do not specify the life of a cookie, then it will last on the client machine till you do not clean it.We can specify the particular date and time when the cookie is automatically removed from the client machine.Cookie are used to transfer the information from one page to another page. You can also disable the cookie on the browser so that the information did not store on your machine.

Syntax of creating cookie:

HTTP Cookie V = new HTTP Cookie(“user”,”ABC”);

Here,
V – Cookie Object
User – Name of the Cookie [Key]
ABC – Value of the Cookie


Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
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)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie v = new HttpCookie(“user”);
        v.Value = “abc”;
        v.Expires = DateTime.Now.AddDays(30);
        Response.Cookies.Add(v);
        Response.Redirect(@“~/Default2.aspx”);
    }
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*