1. Create a cookie
<script runat= "Server" >
protected void btnAdd_Click (object sender, EventArgs e)
{
response.cookies["message"]. Value = txtcookievalue.text;//Creates a cookie for the contents of the text box input
}
</script>
<form id= "Form1" runat= "Server" >
<div >
<asp:label id= "Lblcookievalue" text= "Cookie Value:" associatedcontrolid= "Txtcookievalue" runat= "Server"/>
<asp:textbox id= "Txtcookievalue" runat= "Server"/>
<asp:button id= "Btnadd" text= "Add Value" onclick= "btnAdd_Click" runat= "Server"/>
</div>
</form>
2. Create a persistent cookie
<script runat= "Server" >
void Page_Load ()
{
Get Current value of cookie
int counter = 0;
if (request.cookies["Counter"]! = NULL)
{
Counter = Int32.Parse (request.cookies["counter"]. Value);
Increment counter
counter++;
ADD Persistent Cookie to browser
response.cookies["Counter"]. Value = counter. ToString ();
response.cookies["Counter"]. Expires = DateTime.Now.AddYears (2);
Display Value of Counter cookie
Lblcounter.text = counter. ToString ();
}
}
</script>
Properties of 3.Cookie
The HttpCookie class represents a cookie. When you create or read a cookie, you can use the following properties
(1) domain-is used to specify the domain name associated with the cookie, the default value is the current domain name;
(2) expires-is used to create a persistent cookie by specifying an expiration time;
(3) haskeys-is used to determine whether the cookie is a multi-valued cookie;
(4) httponly-is used to avoid cookies being accessed by JavaScript;
(5) name-The name of the user-specified cookie;
(6) The path-is used to specify the path associated to the cookie. The default is/.
(7) secure-is used to specify that the cookie needs to be passed through an SSL connection;
(8) value-allows the value of the read/write cookie;
(9) values-when using a multi-valued cookie, read/write a specific value;
Using browser cookies