Cookies in asp.net with examples

We will discuss here, what are cookies, why we need cookies, what are the limitation of cookies, when we need to use cookies with example code in asp.net and many more.

what are cookies: HTTP Cookie, Web Cookie, Browser Cookie, Session Cookie, etc. are some names of the cookies. Cookies are small text file sent by web server and saved by browser on client machine.

We need cookies to store user preferences on client machine. Say we have a page with many search criteria, so one user can select all his criteria and search the result, at the same time we can save all the criteria so next time with user will open this page we can automatically select his previous selection.

Limitations:

  • Can store only string
  • Cannot store object say we have a dataset object want to save in cookies, no we cannot save object in cookie
  • Most browsers support cookies of up to 4096 bytes.
  • Most browsers allow only 20 cookies per site, if you try to store more, the oldest cookies are discarded
  • User might be set his browser not to accept the cookies
  • User can delete cookies.

When to use cookies and when to not:

  • When data is not critical importance
  • We should not save password in cookies
  • We should not save any data in cookies which we don’t want to share with user.
  • As we discussed above to save the search criteria, that’s good, we can save these kinds of data

Now we discussed about the ups and downs of cookies, time to write some code to understand how we can use it. To save a single value in cookie for 30 days

Response.Cookies["userName"].Value = "john";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(30);

Read a single value cookie

if(Request.Cookies["userName"] != null)
   Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);

To save multiple values in cookies for 30 days

Response.Cookies["Info"]["fName"] = "John";
Response.Cookies["Info"]["lNake"] = "Oliver";
Response.Cookies["Info"]["city"] = "Jersey City";
Response.Cookies["Info"]["state"] = "NJ";
Response.Cookies["Info"].Expires = DateTime.Now.AddDays(30);
// OR
HttpCookie aCookie = new HttpCookie("Info");
aCookie.Values["fName"] = "John";
aCookie.Values["lName"] = "Oliver";
aCookie.Values["city"] = "Jersey City";
aCookie.Values["state"] = "NJ";
aCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(aCookie);

Read above multi-value cookie

if (Response.Cookies["Info"] != null)
{
   System.Collections.Specialized.NameValueCollection info =
           Response.Cookies["Info"].Values;
   String fName = info["fName"];
   String lName = info["lName"];
   String city = info["city"];
   String state = info["state"];
} 

All these are the basic information about the cookies, cookies are not used mostly in our application but we must know the basics of cookies at-least whatever we discussed here.

Myghty .Net developer
  • cookies
  • asp.net
By Myghty On 26 May, 13  Viewed: 2,158

Other blogs you may like

Upload multiple image in multiple size with progress bar in asp.net

In asp.net there is not control to select multiple files and upload them once with progress bar, so we will use a small third party DLL to achieve this functionality. We will use Flajaxian FileUploader, you can download it from [http://www.flajaxian.com][1] We will create three different images... By Hamden   On 12 Jul 2012  Viewed: 5,952

Check/Uncheck all checkboxes in asp.net by javascript

I was searching for select/deselect all checkboxes code into a gridview. I found most of them only provide to select and deselet all the checkboxes if any of the checkbox is unselected into grid the main checkbox is not affecting. I further try to search some useful code but could not found any... By Ali Adravi   On 10 Jul 2012  Viewed: 4,590

MVC model validation for email from database in asp.net

There are many cases where we need to validate the value in controls before submitting to the database, so let's discuss email validation like format, length and existence into database. We will walk through in this article, how to validate entered user email from database, whether email is already... By Ali Adravi   On 05 Jan 2013  Viewed: 6,253

Routing with asp.net web forms application

Microsoft .NET Framework 3.5 SP1 introduced a routing engine to the ASP.NET runtime. The routing engine can decouple the URL in an incoming HTTP request from the physical Web Form that responds to the request, allowing you to build friendly URLs for your Web applications. Let's start make our... By Ali Adravi   On 05 Jan 2013  Viewed: 2,212

Log error in a text file in ASP.Net

It’s good practice to log all the error into a file or database so in future you can check, how your application is working, and is there any issue which is not caught by Q.A. team. Really it's helpful to understand and caught the issues after deploying the application on production... By Myghty   On 30 Dec 2012  Viewed: 1,246