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

div class=”post-content”>

Cookies in Asp.Net :

A cookie is a message given to a Web browser by a Web server. The browser stores the message in a small text file that the server embeds on the user’s computer. Every time the same computer requests a page with a browser, the cookie is sent back to the server too.

Recommended Reading : Asp.Net MVC Overview

Asp.Net Cookies

Initial State Of Cookie Creation

For example, when visitor comes to your web site you can store information about last visit and retrieve that information when visitor comes next time.

How to create a Cookie ? 

In order to create a Cookie, we need to use the Response.Cookies command. In the following example we will create a cookie named “username” and assign the value “someValue” to it:

Example Cookies Creation :

<% Response.Cookies(“username”) = “Dev” Response.Cookies(“username”).Expires = Date + 100 %>

Get Your Dot Net Certification With 20% Off – ENROLL NOW

How to Retrieve a Cookie Value? 

To retrieve in Asp .Net cookies we use Request.Cookie. Here the cookie is assigned to and you need to retrieve the data . In order to retrieve the value of the cookie, you needs to use the Request.Cookie command.

Example  Cookies Retrieval :

<% username = Request.Cookies(“username”) response.write(“Username: ” & username) %>

Cookie with Multiple Values :

Cookie can also contain multiple values, using something called keys. Take a look at this example.

<% Response.Cookies(“client”)(“name”) = “Dev” Response.Cookies(“client”)(“username1”) = “Dev123” Response.Cookies(“client”)(“age”) = “27” %>
<% realname = Response.Cookies(“client”)(“name”) username = Response.Cookies(“client”)(“username1”) age = Response.Cookies(“client”)(“age”) %>

Trending Now : Advanced Features of Dot Net framework

Advantages of Cookies :

  •  Asp.Net cookies are simple to use.
  • Light in size, and it occupy less memory.
  • Cookie is Stored server information on client side.

Disadvantages of Cookies :

  • Cookie is don’t work if security level is set very high in browser.
  • Cookie is stored on the client side computer in plain text, its not secure.
  • The Limited Number of Cookie is Stored.

You may also like :