COOKIES IN ASP.NET
A cookie is a small amount of data which is either stored at client side in text file or in memory of the client browser session.
Cookies are always sent with the request to the web server and information can be retrieved from the cookies at the web server.
Cookie is a small piece of information stored as a string. Web server sends the cookie and browser stores it, next time server returns that cookie .Cookies are mostly used to store the information about the user.
Cookies are stores on the client side .To store data in a cookie is not secure due to it's location at client end.
//Storing value in cookie
HttpCookie cookie = new HttpCookie("Name");
cookie.Value = "Nitish";
//Retrieving value in cookie
if (Request.Cookies.Count > 0 && Request.Cookies["Name"] != null)
lblName.Text = "Welcome" + Request.Cookies["Name"].ToString();
lblName.Text = "Welcome Guest";
TYPES OF COOKIES
There are two types of cookies, These are:
1) PERSISTENCE COOKIES
Cookies having an expiration date is called a persistent cookie. This type of cookie reaches their end as their expiration dates comes to an end. In this cookie we set an expiration date.
Persistence cookies are permanently stored till the time you set.
HttpCookie aCookieValPer = new HttpCookie("Persistance");
aCookieValPer.Value = "This is A Persistance Cookie";
aCookieValPer.Expires = DateTime.Now.AddSeconds(10);
2) NON-PERSISTENCE COOKIE
Non persistence cookies are not permanently stored on the user client hard disk folder. It maintains user information as long as the user accesses the same browser. When user closes the browser the cookie will be discarded. Non Persistence cookies are useful for public computers.
HttpCookie aCookieValNonPer = new HttpCookie("NonPersistance");
aCookieValNonPer.Value = "This is A Non Persistance Cookie;
Response.Cookies.Add(aCookieValNonPer);how to create cookie :
ADVANTAGE OF COOKIES
• Cookies do not require any server resources since they are stored on the client.
• Cookies are easy to implement.
• You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
DISADVANTAGE OF COOKIES
• Users can delete a cookies.
• Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.
For Share :-