In this article, I will demonstrate you what is cookie in asp.net and why we use it.
What is Cookie?
Cookie is less memory space on web server that is used to store the small data. So, basically cookie is small text file that is created by web server and saved it by browser.
It is used to store the small amount of data on the browser when someone is visiting a site using browser. The common purpose to use cookie is to remember the user who has already visited. So, It maintain the personal information on the client machine within the browser.
Is this Secured?
Sometime, it is also used to authenticate the user information. But Cookie can be stolen by hackers because of it is simple text file to store small data. So, it is not secured.
There are two types of cookies
- In Memory Cookie
- Persistent Cookie
In Memory Cookie
As it is stored inside the browse memory process so it is called In Memory Cookie. It is temporary cookie, if you close the browser than cookie auto deleted. This cookie data can be accessible to all the pages within the same client request. In C# HttpCookie class is used to send and read the cookie.
Create Cookie
HttpCookie MyCookie = new HttpCookie("MyCookie");
MyCookie.Value = txtSetCookie.Text;
MyCookie.Expires = DateTime.Now.AddHours(1);
Response.Cookies.Add(MyCookie);
Read Cookie
string cookievalue = Request.Cookies["MyCookie "].Value;
Persistent Cookie
It is different types of cookie. It is stored in client hard disk. This is maintain by client system that is why called persistent cookie.
HttpCookie class contains some list of propertie..
Expires : It is expiration time of cookie.
HasKeys: If the cookie has subkeys than it will be true.
Name: It is the name of cookie.
Value: It is the value of cookie.
Conclusion:
So, today you learned what is cookie and why we use it. Also you learned the type of cookie and their benefit.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks