Reading/Writing Cookies Reading/Writing Cookies asp.net asp.net

Reading/Writing Cookies


You can't use the Global.asax constructor to add a cookie to the Response because the Global.asax constructor is called before the application starts processing the HTTP request.

Move your code from the Global.asax constructor to the Application_BeginRequest method:

public void Application_BeginRequest(){    myCookie = new HttpCookie("UserSettings");    myCookie.Value = "nl";    myCookie.Expires = DateTime.Now.AddDays(1d);    Response.Cookies.Add(myCookie);}

The Global.asax has a number of different events that are fired, you just chose wrongly.

  • Application_Init: Fires when the application initializes for the first time.
  • Application_Start: Fires the first time an application starts.
  • Session_Start: Fires the first time when a user’s session is started.
  • Application_BeginRequest: Fires each time a new request comes in.
  • Application_EndRequest: Fires when the request ends.
  • Application_AuthenticateRequest: Indicates that a request is ready to be authenticated.
  • Application_Error: Fires when an unhandled error occurs within the application.
  • Session_End: Fires whenever a single user Session ends or times out.
  • Application_End: Fires when the application ends or times out (Typically used for application cleanup logic).

(from http://en.wikipedia.org/wiki/Global.asax)


cookies is a small piece of information stored on the client machine. This file is located on client machines.Its is used to store user preference information like Username, Password,City and PhoneNo etc on client machines. We need to import namespace called Systen.Web.HttpCookie before we use cookie.

Type of Cookies? Persist Cookie - A cookie has not have expired time Which is called as Persist Cookie

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

    HttpCookie userInfo = new HttpCookie("userInfo");    userInfo["UserName"] = "Jishan siddique";    userInfo["UserColor"] = "Black";    userInfo.Expires.Add(new TimeSpan(0, 1, 0));    Response.Cookies.Add(userInfo);


Cookie's common property:

1.Domain => Which is used to associate cookies to domain.2.Secure  => We can enable secure cookie to set true(HTTPs).3.Value    => We can manipulate individual cookie.4.Values  => We can manipulate cookies with key/value pair.5.Expires => Which is used to set expire date for the cookies. 

Advantages of Cookie:

1.Its clear text so user can able to read it.2.We can store user preference information on the client machine.3.Its easy way to maintain.4.Fast accessing.

Disadvantages of Cookie

1.If user clear cookie information we can't get it back.2.No security.3.Each request will have cookie information with page. 

How to clear the cookie information?

1.we can clear cookie information from client machine on cookie folder

2.To set expires to cookie objectuserInfo.Expires = DateTime.Now.AddHours(1);It will clear the cookie with one hour duration