.NETGURU
Retrieving multiple settings from a cookie
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcs' list.


Julian Voelcker
I think that I am going mad here.

I have no problems setting a cookie value and then retrieving it.

I can also set multiple values to a cookie without any problems.

However I cannot get my head around retrieving multiple values from a
cookie.

Any chance of someone providing some sample code?

Also, with Classic ASP one could set the domain name for the cookie so
that it was only accessible from that domain - how can I set or disable
this using C#?

Cheers,

Julian Voelcker
The Virtual World (UK) Limited
Cirencester, United Kingdom

Reply to this message...
 
    
Brian W. Spolarich

| I think that I am going mad here.

Join the club. :-)
=20
| I have no problems setting a cookie value and then retrieving it.
|=20
| I can also set multiple values to a cookie without any problems.
|=20
| However I cannot get my head around retrieving multiple values from a=20
| cookie.
|=20
| Any chance of someone providing some sample code?

How about this?

        public void SetCookie()
        {
            HttpCookie myCookie =3D new HttpCookie("test");
            myCookie.Domain =3D "localhost";
            myCookie.Values.Add("foo","bar");
            myCookie.Values.Add("fred","wilma");
            myCookie.Values.Add("barney","betty");

            Response.Cookies.Add(myCookie);
        }

        public void GetCookie()
        {
            HttpCookie aCookie =3D Response.Cookies["test"];
            foreach (string s in aCookie.Values.Keys)
            {
                Response.Write("Key: " + s + " Value: " + aCookie.Values[s] + =
"<P>");
            }
        }

Some things to think about:

- Response.Cookies is of type HttpCookieCollection which implements =
the IEnumerable and IDictionary interfaces, which enables you to =
retrieve objects from the collection by index (e.g. Response.Cookies[1]) =
or by key name. This is all provided by the objects and interfaces =
defined in the System.Collections namespace.
- HttpCookie provides a number of properties which map to the various =
cookie attributes you're familiar with:
    - Domain
    - Expires
    - Name
    - Path

- HttpCookie has a property called Values, which is of type =
NameValueCollection, and contains pairs of names and values. As such =
you can iterate through its keys in the way I've identified above, or =
you can access them directly by name or index. e.g.:
    aCookie.Values[0];
    aCookie.Values["username"];

    and also, for convenience sake:

    aCookie["username"];

    The last one is true because in C# you can declare a particular =
property to be the indexer for a class.
=20
| Also, with Classic ASP one could set the domain name for the=20
| cookie so=20
| that it was only accessible from that domain - how can I set=20
| or disable=20
| this using C#?

Declare the HttpCookies object and use the 'Domain' property.

Regards,

-bws

Reply to this message...
 
    
Brian W. Spolarich
| -----Original Message-----
| From: Julian Voelcker [mailto:Click here to reveal e-mail address]
| Sent: Wednesday, July 24, 2002 12:59 PM
| To: Brian W. Spolarich
| Subject: Re: [aspngcs] Retrieving multiple settings from a cookie
|=20
|=20
| Thanks Brian, you are a star. I really must get my head around these=20
| collections. It just seemed to be so much easier with Classic ASP.

I'd argue that this is an example of "front-loading" of effort and =
complexity: its "harder" to do simple things, but its comparatively =
"easier" to do more complex things. =20

Classic ASP doesn't require a great deal of overhead to do simple =
things, but also doesn't provide a great deal of support when you want =
to do something more complex or involved. Thus you have to "roll your =
own" when you want to do something non-trivial.

The collections stuff sort of works like magic. Its almost always =
there when you want it to be. The trick for me now is to figure out =
when to use it in my own classes, and how to implement it properly.

I've found the learning curve associated with the .NET framework class =
library to be rather steep, and learning C#'s differences from Java =
increases the challenge. Add to that ASP.NET's page processing model, =
quite different from good-old CGI, and you've got a lot of stuff to =
learn, unlearn, and occasionally relearn.

However I think the benefits outweigh the drawbacks, as I believe =
developers can become very productive once they achieve a critical mass =
of knowledge and experience. I'm not sure I'm there yet myself, but I =
do enjoy helping others solve problems. Anyways, other people's =
problems are always more interesting that one's own (witness the growth =
of the psychotherapy industry!).

Peace,

-bws

Reply to this message...
 
 
System.Collections.IDictionary
System.Collections.IEnumerable
System.Collections.Specialized.NameValueCollection
System.Web.HttpCookie
System.Web.HttpCookieCollection




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification