Setting and retrieving cookies in C # code

I am trying to save a dropdown selection to a cookie on 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 great and I can see it in the response object with quickwatch.

I am trying to get it on 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?

+3


source to share


1 answer


Your code looks fine.

The problem might be that the server never sends the cookie to the browser, or the browser doesn't handle the cookie as you would like.

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

If the server is not sending the cookie, then check the following:



  • Find the file web.config

    for the httpCookies section . If present, check the setting requireSSL

    . If the parameter is requireSSL

    set to true (i.e. Https), but the website tries to send a cookie over plain http, the cookie will not be sent to the browser.
  • Maybe some cookie deleting code before ASP.Net finishes processing the page. Find 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 cookie is handling the cookie correctly. Try using at least two different types of browsers (e.g. Internet Explorer, Google Chrome, FireFox, etc.):

  • Check the date and time of the computer on which the browser is running. If the date / time of the computer is set to a point past the expiration date of the cookie, the browser will consider the cookie expired and will not return it to the server.
  • Check your browser settings and make sure it accepts cookies.
  • As suggested in the comments by @ mituw16, open the website related to the cookie and use the browser developer tools to check the cookies and see if your cookie is present.
  • If there is no cookie, try building a very simple application as described in ASP.NET Cookies Overview (scroll down to "Determining whether your browser accepts cookies").
  • If the cookie is still missing from the browser, your browser may have an error, especially IE 11. IE 11 will not store cookies if the cookie domain contains an underscore character (for example 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 the User Agent string, which leads to even other problems with the cookie.
+2


source







All Articles