Http​Cookie Http​Cookie Http​Cookie Class

Definition

Provides a set of properties and methods to manage an HTTP cookie.

public : sealed class HttpCookie : IStringable, IHttpCookiepublic sealed class HttpCookie : IStringable, IHttpCookiePublic NotInheritable Class HttpCookie Implements IStringable, IHttpCookie
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Cookies are small chunks of data that are passed between HTTP servers and clients. Their primary use is by the web server developers to save session state. The web server developer uses the state to correlate earlier and later requests. For example, session state can include a unique visitor id which is then used to remember (on the server side) the visitor’s shopping cart.

The HTTP server sends cookies to the client with the Set-Cookie header. The client sends them back with the Cookie header. Cookies that are sent back don’t include cookie attributes. The correct cookie usage is that the server sets a cookie once and doesn’t re-set it on subsequent responses. This works because a cookie, once set, will be sent back until it expires or is deleted (as long as it matches the URI correctly). Cookies are persistent when they have a valid Expires header. Cookies that are not persistent are session cookies, and are not persisted from one session to another.

Although cookies are commonly used by web services developers, they are sometimes used by client-side developers.

Constructors

HttpCookie(String, String, String) HttpCookie(String, String, String) HttpCookie(String, String, String)

Initializes a new instance of the HttpCookie class with a specified name, domain, and path.

public : HttpCookie(PlatForm::String name, PlatForm::String domain, PlatForm::String path)public HttpCookie(String name, String domain, String path)Public Sub New(name As String, domain As String, path As String)
Parameters
name
PlatForm::String String String

The name for the HttpCookie

domain
PlatForm::String String String

The domain for which the HttpCookie is valid.

path
PlatForm::String String String

The URIs to which the HttpCookie applies.

Attributes

Remarks

The name parameter of the HttpCookie can't be set to null (Nothing in Visual Basic).

The property should be a token as defined in RFC 2616 and RFC 6265.

The following characters are used as separators and can't be used in the name parameter:

  • left paren ('(')
  • right paren (')')
  • greater than ('>')
  • less than ('<')
  • at sign ('@')
  • comma (',')
  • semicolon (';')
  • colon (':')
  • backslash ('\')
  • double quote ('"')
  • forward slash ('/')
  • left square bracket ('[')
  • right square bracket (']')
  • question mark ('?')
  • equal sign ('=')
  • left squiggly bracket ('{')
  • right squiggly bracket ('}')
  • new line ('\n')
  • return ('\r")
  • space (' ')
  • tab ('\t")

The domain * parameter specifies those hosts to which the HttpCookie will be sent. A cookie with a *domain * parameter of *.contoso.com will be sent to the contoso.com domain and all subdomains (www.contoso.com and info.contoso.com, for example).

A cookie with a domain * parameter of *info.contoso.com will not be sent to contoso.com or www.contoso.com, but will be sent to info.contoso.com and all subdomains (uk.info.contoso.com and us.info.contoso.com, for example).

If the domain parameter is not specified, then the HttpCookie will be sent only to the host and subdomains of the host that this cookie was received from.

The path parameter specifies the subset of paths on the origin server to which this HttpCookie applies. If this parameter is not specified, the default path matches any path and the HttpCookie will be sent on all HTTP requests to the *domain * parameter specified and all subdomains.

Properties

Domain Domain Domain

Get the domain for which the HttpCookie is valid.

public : PlatForm::String Domain { get; }public string Domain { get; }Public ReadOnly Property Domain As string
Value
PlatForm::String string string

The domain for which the HttpCookie is valid.

Attributes

Remarks

The Domain property specifies those hosts to which the cookie will be sent.

A cookie with a Domain property of .contoso.com will be sent to the contoso.com domain and all subdomains (www.contoso.com and info.contoso.com, for example).

A cookie with a Domain property of info.contoso.com will not be sent to contoso.com or www.contoso.com, but will be sent to info.contoso.com and all subdomains (uk.info.contoso.com and us.info.contoso.com, for example).

The default value for the Domain property is the host name that this cookie was received from.

Expires Expires Expires

Get or set the expiration date and time for the HttpCookie.

public : IReference<DateTime> Expires { get; set; }public Nullable<DateTimeOffset> Expires { get; set; }Public ReadWrite Property Expires As Nullable<DateTimeOffset>
Value
IReference<DateTime> Nullable<DateTimeOffset> Nullable<DateTimeOffset>

The expiration date and time for the HttpCookie.

Attributes

Remarks

Setting the Expires property to null makes this a Cookie that is only good for the current HTTP session and will not be persisted. Setting the Expires property to a date in the past deletes the Cookie.

The default value for the Expires property is null.

Javascript and .NET languages do not use DateTime directly. In Javascript a DateTime is projected as a Date object, and in .NET it is projected as a System.DateTimeOffset structure. In C++, the DateTime.UniversalTime member has the same granularity as a and supports the date ranges required by Javascript and .NET.

HttpOnly HttpOnly HttpOnly

Get or set a value that controls whether a script or other active content can access this HttpCookie.

public : PlatForm::Boolean HttpOnly { get; set; }public bool HttpOnly { get; set; }Public ReadWrite Property HttpOnly As bool
Value
PlatForm::Boolean bool bool

Whether a script or other active content can access this HttpCookie.

true if a script or other active content cannot access this HTTP cookie; otherwise, false. The default is false.

Attributes

Remarks

When this property is set to true, a script or other active content cannot access this HTTP cookie.

Name Name Name

Get the token that represents the HttpCookie name.

public : PlatForm::String Name { get; }public string Name { get; }Public ReadOnly Property Name As string
Value
PlatForm::String string string

The token that represents the HttpCookie name.

Attributes

Remarks

The Name property must be set when HttpCookie is initialized by the constructor.

The Name of the HttpCookie can't be null (Nothing in Visual Basic).

The property should be a token as defined in RFC 2616 and RFC 6265.

The following characters are used as separators and can't be used in the cookie name:

  • left paren ('(')
  • right paren (')')
  • greater than ('>')
  • less than ('<')
  • at sign ('@')
  • comma (',')
  • semicolon (';')
  • colon (':')
  • backslash ('\')
  • double quote ('"')
  • forward slash ('/')
  • left square bracket ('[')
  • right square bracket (']')
  • question mark ('?')
  • equal sign ('=')
  • left squiggly bracket ('{')
  • right squiggly bracket ('}')
  • new line ('\n')
  • return ('\r")
  • space (' ')
  • tab ('\t")

Path Path Path

Get the URI path component to which the HttpCookie applies.

public : PlatForm::String Path { get; }public string Path { get; }Public ReadOnly Property Path As string
Value
PlatForm::String string string

The URI path component to which the HttpCookie applies.

Attributes

Remarks

The Path property specifies the subset of paths on the origin server to which this HttpCookie applies. If this property is not specified, the default path matches any path and the HttpCookie will be sent on all HTTP requests to the Domain.

Secure Secure Secure

Get or set the security level for the HttpCookie.

public : PlatForm::Boolean Secure { get; set; }public bool Secure { get; set; }Public ReadWrite Property Secure As bool
Value
PlatForm::Boolean bool bool

The security level for the HttpCookie.

true if the client is only to return the cookie in subsequent requests if those requests use HTTPS; otherwise, false. The default is false.

Attributes

Remarks

When this property is true, this HTTP cookie may be sent only with https:// requests. This provides additional security since this prevents network sniffing the value of the cookie.

Value Value Value

Get or set the value for the HttpCookie.

public : PlatForm::String Value { get; set; }public string Value { get; set; }Public ReadWrite Property Value As string
Value
PlatForm::String string string

The value for the HttpCookie.

Attributes

Remarks

The Value of the HttpCookie must not be set to null (Nothing in Visual Basic).

The following characters are reserved and cannot be used for this property:

  • backslash ('\')
  • comma (',')
  • semicolon (';')

Methods

ToString() ToString() ToString()

Returns a string that represents the current HttpCookie object.

public : PlatForm::String ToString()public string ToString()Public Function ToString() As string
Returns
PlatForm::String string string

A string that represents the current object.

Attributes

Remarks

The ToString method returns a string that matches a Set-Cookie HTTP header suitable for including on a request message.