Retrieving cookie value data RRS feed

  • Question

  • User-600945157 posted


    My web application creates a number of cookies on a button click within a device list (generated by a listview), these cookies contain some data and are created successfully, using the below code

    HttpCookie cookie = new HttpCookie(DeviceNo);

    This means that each cookie created is given the name of the items Device number, so all cookies have a different name 

    The issue i am having is retrieving the cookies 

    The code used to retrieve the cookie data is;

                if (Request.Cookies["cookie"] != null)
                    var cookie = Request.Cookies["cookie"].Value;
                    Quote.Text = Environment.NewLine + cookie;

    This code returns no data, however if i change "Request.Cookies["cookie"] to "Request.Cookies["nameOfIndividualCookie"]" Then i am able to get the data from that individual cookie.

    Is there any way to gather the data from all cookies which are created using "HttpCookie cookie = new HttpCookie(DeviceNo);" section of code?

    Many thanks

    Tuesday, February 17, 2015 10:15 AM

All replies

  • User-760709272 posted

    You can use individual values inside a single cookie

    Response.Cookies["MyData"]["device1"] ....

    Response.Cookies["MyData"]["device2"] ....

    use similar code to read them back

    Tuesday, February 17, 2015 10:39 AM
  • User475983607 posted

    IMHO, it's better to name cookies.  Here's how to enumerate over the cookie collection.  The code sample assumes you have a label1 control on the page to hold the results.

            protected void PrintCookiesData()
                StringBuilder sb = new StringBuilder();
                HttpCookieCollection cookieCollection =  Request.Cookies;
                foreach (string key in cookieCollection.Keys)
                    sb.Append("Cookie retrieved from client. <br/>");
                    sb.Append("Cookie Name: " + cookieCollection[key].Name + "<br/>");
                    sb.Append("Cookie Value: " + cookieCollection[key].Value + "<br/>");
                    sb.Append("Cookie Expiration Date: " +
                    cookieCollection[key].Expires.ToString() + "<br/>");
                Label1.Text = sb.ToString();

    Tuesday, February 17, 2015 10:56 AM
  • User2008642861 posted

    Hi Martin91,

    As mgebhard said, you'd better to use below code to get your data.


    In the MSDN's library, it has shown you an example to refer. 

    For more information, I suggest that you could refer to below link.


    Hope it's helpful to you.

    Best regards,


    Wednesday, February 18, 2015 3:40 AM
  • User-600945157 posted


    Thanks for the replies, Im still having issues with this. Apologies if i am repeating here, each time a cookie is created it is given the name of the Device Number which is held in a database. This means there are hundreds of possible names which could be given to the cookies (however it is highly unlikely a customer would create more than 10 cookies when using the site, the cookies are set to last 1 hour and would be expired when the user submits the quote).

    If when the cookies are created, can they be added to a cookie collection, which can then be retrieved  using the Request.Cookies method? 

    //create cookie
    HttpCookie cookie = new HttpCookie(PartNo);
    //add the cookie

    Wednesday, February 18, 2015 5:38 AM
  • User2008642861 posted

    Hi Martin91,

    As far as I know, you could use HttpRequest.Cookies Property. This property gets a collection of cookies sent by the client.

    For more information, please refer to the below link.


    Also, for the Request.Cookies Collection, I suggest that you could refer to the below link.


    Best regards,


    Sunday, February 22, 2015 1:04 AM