System.Net.Cookie Class

Assembly: System.dll
Namespace: System.Net
Summary
Provides a set of properties and methods used to manage cookies. This class cannot be inherited.
C# Syntax:
[Serializable]
public sealed class Cookie
Remarks
The Cookie class is used by a client application to retrieve information about cookies received with HTTP responses. The following cookie formats are supported during parsing the HTTP resonse headers: Netscape, RFC 2109, and RFC 2965.

For a list of initial property values for an instance of Cookie, see the various Cookie constructors.

See also:
System.Net Namespace | CookieCollection | CookieContainer | CookieException

System.Net.Cookie Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the Cookie class.
ctor #2 Overloaded:
.ctor(string name, string value)

Initializes a new instance of the Cookie class with a specified Cookie.Name and Cookie.Value.
ctor #3 Overloaded:
.ctor(string name, string value, string path)

Initializes a new instance of the Cookie class with a specified Cookie.Name, Cookie.Value, and Cookie.Path.
ctor #4 Overloaded:
.ctor(string name, string value, string path, string domain)

Initializes a new instance of the Cookie class with a specified Cookie.Name, Cookie.Value, Cookie.Path, and Cookie.Domain.
Public Properties
Comment Read-write

Gets or sets a comment that the server can add to a Cookie.
CommentUri Read-write

Gets or sets a URI comment that the server can provide with a Cookie.
Discard Read-write

Gets or sets the discard flag set by the server.
Domain Read-write

Gets or sets the URI for which the Cookie is valid.
Expired Read-write

Gets or sets the current state of the Cookie.
Expires Read-write

Gets or sets the expiration date and time for the Cookie as a DateTime.
Name Read-write

Gets or sets the name for the Cookie.
Path Read-write

Gets or sets the URIs to which the Cookie applies .
Port Read-write

Gets or sets a list of TCP ports that the Cookie applies to.
Secure Read-write

Gets or sets the security level of a Cookie.
TimeStamp Read-only

Gets the time when the cookie was issued as a DateTime.
Value Read-write

Gets or sets the Cookie.Value for the Cookie.
Version Read-write

Gets or sets the version of HTTP state maintenance to which the cookie conforms.
Public Methods
Equals Overridden:
Overrides the Object.Equals method.
GetHashCode Overridden:
Overrides the Object.GetHashCode method.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString Overridden:
Overrides the Object.ToString method.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Net.Cookie Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the Cookie class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public Cookie();
Remarks
The default constructor initializes all fields to their default values, using empty strings ("") for name, value, path, and domain. Note that at least the Cookie.Name property must be initialized before using an instance of the Cookie class.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the Cookie class with a specified Cookie.Name and Cookie.Value.
C# Syntax:
public Cookie(
   string name,
   string value
);
Parameters:

name

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t). The dollarsign character cannot be the first character. The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t). The dollarsign character cannot be the first character.

value

The value of a Cookie. The following characters must not be used inside value: semicolon, comma.

Remarks
The default for the value parameter uses the empty string ("") .
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the Cookie class with a specified Cookie.Name, Cookie.Value, and Cookie.Path.
C# Syntax:
public Cookie(
   string name,
   string value,
   string path
);
Parameters:

name

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t). The dollarsign character cannot be the first character.

value

The value of a Cookie. The following characters must not be used inside value: semicolon, comma.

path

The subset of URIs on the origin server to which this Cookie applies. The default value is "/".

Remarks
The default for the path parameter uses the empty string ("") .
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the Cookie class with a specified Cookie.Name, Cookie.Value, Cookie.Path, and Cookie.Domain.
C# Syntax:
public Cookie(
   string name,
   string value,
   string path,
   string domain
);
Parameters:

name

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t). The dollarsign character cannot be the first character.

value

The value of a Cookie object. The following characters must not be used inside value: semicolon, comma.

path

The subset of URIs on the origin server to which this Cookie applies. The default value is "/".

domain

The optional internet domain for which this Cookie is valid. The default value is the host this Cookie has been received from.

Remarks
The default for the domain and path parameters uses the empty string ("") .
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Comment (read-write)
Summary
Gets or sets a comment that the server can add to a Cookie.
C# Syntax:
public string Comment {get; set;}
Remarks
The client can inspect this optional comment for information added by the server. For example, the server could include information about issues such as the privacy policy or intended usage.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: CommentUri (read-write)
Summary
Gets or sets a URI comment that the server can provide with a Cookie.
C# Syntax:
public Uri CommentUri {get; set;}
Remarks
The URI can provide optional information, such as how the server uses the Cookie.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Discard (read-write)
Summary
Gets or sets the discard flag set by the server.
C# Syntax:
public bool Discard {get; set;}
Remarks
When true, this property instructs the client application not to save the Cookie on the user's hard disk when a session ends.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Domain (read-write)
Summary
Gets or sets the URI for which the Cookie is valid.
C# Syntax:
public string Domain {get; set;}
Remarks
A server cannot indicate a domain other than its own URI. However, it can indicate more than one server in the domain. The default value is the host this cookie has been received from.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Expired (read-write)
Summary
Gets or sets the current state of the Cookie.
C# Syntax:
public bool Expired {get; set;}
Remarks
Expired Cookies, if received, should be destroyed by the client application.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Expires (read-write)
Summary
Gets or sets the expiration date and time for the Cookie as a DateTime.
C# Syntax:
public DateTime Expires {get; set;}
Remarks
Setting the Cookie.Expires property to DateTime.MinValue means that the Cookie never expires.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Name (read-write)
Summary
Gets or sets the name for the Cookie.
C# Syntax:
public string Name {get; set;}
Remarks
The Cookie.Name property must be initialized before using an instance of the Cookie class. The following characters are reserved and cannot be used for this attribute value: equal sign, semicolon, comma, newline (\n),return (\r), tab (\t). The dollarsign ($) character cannot be the first character.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Path (read-write)
Summary
Gets or sets the URIs to which the Cookie applies .
C# Syntax:
public string Path {get; set;}
Remarks
The Cookie.Path property specifies the subset of URIs on the origin server to which this Cookie applies. If not specified then this Cookie will be sent to all pages on the origin server or servers.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Port (read-write)
Summary
Gets or sets a list of TCP ports that the Cookie applies to.
C# Syntax:
public string Port {get; set;}
Remarks
This attribute restricts the ports to which this Cookie may be sent. The default value means no restriction. Setting this to the empty string ("") will restrict the port to the one used in the HTTP response. Otherwise the value must be a double-quoted string that contains port values delimited with commas.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Secure (read-write)
Summary
Gets or sets the security level of a Cookie.
C# Syntax:
public bool Secure {get; set;}
Remarks
In effect, when this property is true this cookie may be sent only with https:// requests.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: TimeStamp (read-only)
Summary
Gets the time when the cookie was issued as a DateTime.
C# Syntax:
public DateTime TimeStamp {get;}
Remarks
This is a read-only property.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Value (read-write)
Summary
Gets or sets the Cookie.Value for the Cookie.
C# Syntax:
public string Value {get; set;}
Remarks
The Cookie.Value of a Cookie must not be null.The following characters are reserved and cannot be used for this property: semicolon, comma.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: Version (read-write)
Summary
Gets or sets the version of HTTP state maintenance to which the cookie conforms.
C# Syntax:
public int Version {get; set;}
Remarks
The default cookie version is Netscape. If the value is explicitly set to 1, than this Cookie must conform to RFC 2109. Note that if a Cookie was created automatically by receiving a Set-Cookie2 HTTP response header, the conformance is set to RFC 2965.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Overridden Method: Equals(
   object comparand
)
Summary
Overrides the Object.Equals method.
C# Syntax:
public override bool Equals(
   object comparand
);
Parameters:

comparand

A reference to a Cookie.

Return Value:
Returns true if the Cookie is equal to comparand. Two Cookie instances are equal if their Cookie.Name, Cookie.Value, Cookie.Path, Cookie.Domain and Cookie.Version properties are equal. Cookie.Name and Cookie.Domain string comparisons are case-insensitive.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Cookie();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Overrides the Object.GetHashCode method.
C# Syntax:
public override int GetHashCode();
Return Value:
The 32-bit signed integer hash code for this instance.
Remarks
Classes that might be used as a key in a hash table must provide this override, because objects that are used as keys in a hash table are required to generate their own hash code through this method.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: ToString()
Summary
Overrides the Object.ToString method.
C# Syntax:
public override string ToString();
Return Value:
Returns a string representation of this Cookie object suitable for including in a HTTP Cookie: request header.
Remarks
The exact format of the string depends on the RFC this cookie conforms to.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.