Home > Interview Questions > ASP.NET > What is a cookie in asp.net?

What is a cookie in asp.net?

by Thamil   on 04/10/2015   Category: ASP.NET   |  Level: Intermediate   |  Views: 985    |  Points: 25     |  Gold 

A cookie in asp.net is a small piece of text, stored in the client’s machine and used to store and retrieve the user specific details. For example, you can store user specific information in the cookie on client machine and when user visits next time, the application would retrieve the specific user information such as user id, authentication information which was stored in the cookie for processing.

Store the values in the cookies
HttpCookie userCookies = new HttpCookie("userCookies");
userCookies.Value = UserID;
userCookies.Expires = DateTime.Now.AddHours(10);

Retrieve the values from cookie:
string UserID = Request.Cookies["userCookies"].Value;

Advantages of cookies:
1. Most of the browser would support the cookies and store the data up to 4096 bytes only.
2. Easy to maintain and implementation the cookies and fast access.
3. Data will be stored on the client machine so cookies do not use server resource to store the information.
4. Allow user to configure the cookie expiration within specific time or when session ends
Disadvantages of cookies
1. Anyone can open the cookie in the client browser and change the data, there is security risk will there.
2. User can able to disable cookies on client browser.
3. Allows only plain text with less storage 4096 bytes/4kb.
4. If user deletes a cookie on the browser then we can’t get values from cookies through application.

« What is Query String in asp.net?
» What are different types of session state in asp.net?
Post Question  |  Question Home

Recent Posts

User Responses

No response found, be the first to review this question.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts