Cookie Constructor (String, String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the Cookie class with a specified Name, Value, and Path.

Namespace:   System.Net
Assembly:  System (in System.dll)

new : 
        name:string *
        value:string *
        path:string -> Cookie


Type: System.String

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

Type: System.String

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

Type: System.String

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

Exception Condition

The name parameter is null.


The name parameter is of zero length.


The name parameter contains an invalid character.


The value parameter is null .

-or -

The value parameter contains a string not enclosed in quotes that contains an invalid character.

The default for the path parameter uses the empty string ("").

The value parameter for a Cookie must not be a null reference (Nothing in Visual Basic). The following characters are reserved and can not cannot be passed in the value parameter unless the string passed in the value parameter is enclosed in double quotes: semicolon, comma. So the following example constructor would succeed but when you try to add this Cookie to a CookieContainer instance with the Add or Add methods the operation will fail and throw an exception:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "123,456", "");

However, the following constructor with these special characters escaped will create a Cookie that can be added to a CookieContainer instance:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "\"123,456\"", "");

The comma character is used as a delimiter between separate cookies on the same line.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 3.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top