Setting and retrieving cookies in c# code behind

I attempt to save the selection of a dropdown to a cookie in the SelectedIndexChanged event.

protected void BranchNumberList_SelectedIndexchanged(object sender, EventArgs e)
{
   HttpCookie myCookie = new HttpCookie("default_Loc", BranchNumberList.SelectedValue);
   myCookie.Expires = DateTime.Now.AddDays(365);
   Response.Cookies.Add(myCookie);
   ViewDate.Enabled = true;
   SelectEverything();
}

myCookie looks fine and I can see it in the response object with a quickwatch.

I try to retrieve it on the next login when this method is called from Page_Load.

private void BranchName()
{
   DatabaseHelpers dh = new DatabaseHelpers();
   DataSet DrpDownSrc = dh.FillBranchSelection(objConn);
   BranchNumberList.DataSource = DrpDownSrc;
   BranchNumberList.DataTextField = "BranchName";
   BranchNumberList.DataValueField = "LocationID";
   BranchNumberList.DataBind();
   BranchNumberList.Items.Insert(0, "Select a branch");
   try
   {
      BranchNumberList.SelectedValue = this.Request.Cookies["default_Loc"].Value;
   }
   catch (Exception)
   {
      BranchNumberList.SelectedIndex = 0;
   }
}

I always get 'this.Request.Cookies["default_Loc"]' is null.

Can anyone see where I am going wrong?

Upvotes: 3

Views: 3989

Answers (1)

Chris R. Timmons
Chris R. Timmons

Reputation: 2197

Your code looks OK.

The problem could either be that the server is never sending the cookie to the browser, or the browser is not handling the cookie as you would like it to.

The first thing I suggest is to determine if the cookie is being sent to the browser. Use a tool like Fiddler or Wireshark to inspect the HTTP traffic between the server and browser.

If the server is not sending the cookie, perform the following checks:

  • Search the web.config file for an httpCookies section. If it's present, check the requireSSL setting. If requireSSL is set to true (i.e. https), but the web site tries to send the cookie over plain http, the cookie won't be sent to the browser.
  • There might be code that's removing the cookie before ASP.Net is finished processing the page. Search your code for things like Cookies.Clear or Cookies.Remove.

If the cookie is being sent to the browser, the next step is to find out if the browser is handling the cookie properly. Try the following in at least two different kinds of browsers (e.g. Internet Explorer, Google Chrome, FireFox, etc.):

  • Check the date and time of the computer the browser is running on. If the computer's date/time is set to a point that's past the cookie's expiration date, the browser will consider the cookie to be expired and won't return it to the server.
  • Check the browser's settings and make sure it accepts cookies.
  • As suggested by @mituw16 in the comments, open the web site associated with the cookie and use the browser's developer tools to inspect the cookies and see if your cookie is present.
  • If the cookie is not present, try creating a very simple app as described in ASP.NET Cookies Overview (scroll down to the "Determining Whether a Browser Accepts Cookies" section).
  • If the cookie still isn't present in the browser, your browser may have a bug, especially IE 11. IE 11 won't store cookies if the cookie's domain contains an underscore (e.g. http://www.my_web_site.com/) (see Internet Explorer Cookie Internals (FAQ)), and IE 11 has problems with cookies in IFrames and modal popups (SO discussion). IE 11 also changed its User Agent string, which causes even more problems with cookies.

Upvotes: 2

Related Questions