What is a Cookie?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With ASP, you can both create and retrieve cookie values.

Recommended Reading : Understanding Asp.Net AJAX Controls

How are Cookies started?

When a client requests to the server, the server sends cookies to the client. The same cookies can be referred to for subsequent requests. For example, if codeproject.com stores the session ID as cookies, when a client hits the first time on the server, the server generates the session ID and sends it as a cookie to the client .

Trending Now : Advanced Features of Dot Net Framework

Type of Cookies?

Ø Persist Cookie — A cookie has not have expired time Which is called as Persist Cokie

Ø Non-Persist Cookie — A cookie has expired time Which is called as Non-Persist Cookie

How to create a cookie?

Its really easy to create a cookie in the Asp.Net with help of Response object or HttpCookie

Example 1:

HttpCookie userInfo = new HttpCookie(“userInfo”);

userInfo[“UserName”] = “Mithun”;

userInfo[“UserColor”] = “White”;

userInfo.Expires.Add(new TimeSpan(0, 1, 0));

Response.Cookies.Add(userInfo);

Example 2:

Response.Cookies[“userName”].Value = “Mithun”;

Response.Cookies[“userColor”].Value = “White”;

FREE PDF DOWNLOAD LINK FOR MICROSOFT DOT NET COURSE CONTENT

Cookie’s common property:

Domain => Which is used to associate cookies to domain.

Secure => We can enable secure cookie to set true(HTTPs).

Value => We can manipulate individual cookie.

Values => We can manipulate cookies with key/value pair.

Expires => Which is used to set expire date for the cookies.

Advantages of Cookies:

Following are the main advantages of using cookies in a web application:

Ø It’s very simple to use and implement.

Ø Browser takes care of sending the data.

Ø For multiple sites with cookies, the browser automatically arranges them.

Disadvantages of Cookies:

The main disadvantages of cookies are:

Ø It stores data in simple text format, so it’s not secure at all.

Ø There is a size limit for cookies data (4096 bytes / 4KB).

Ø The maximum number of cookies allowed is also limited. Most browsers provide limits the number of cookies to 20. If new cookies come, the old ones are discarded. Some browsers support up to 300.

Ø We need to configure the browser. Cookies will not work on a high security configuration of the browser.

Reference:

Asp.Net Cookies Overview — Easy Guide to Asp.Net Cookies