AppendCookie Method

Syntax

Context.Response.AppendCookie as V (Cookie as System::Web::HttpCookie)

Arguments

CookieSystem::Web::HttpCookie

The cookie to add. Commonly used properties are listed below. For further information about available properties and methods, visit the System::Web::HttpCookie documentation.

DomainCharacter

The cookie's domain.

ExpiresTime

The cookie's expiration date and time (on the client).

HttpOnlyLogical

Specifies whether or not the cookie is available client-side. See Mitigating Cross-site Scripting With HTTP-only Cookies (Microsoft) to learn more about using this property.

NameCharacter

The cookie's name.

SameSiteSystem::Web::SameSiteMode

The value of the SameSite attribute for the cookie. Can be System::Web::SameSiteMode::Lax, System::Web::SameSiteMode::None, or System::Web::SameSiteMode::Strict.

If the SameSite value is not set, i.e. "Unset", the browser will decide how to handle the cookie. Most desktop browsers will treat the cookie as "Lax" while some browsers on mobile devices may treat the cookie as "None" without the SSL requirement.

You must use Unset if you are deploying Cordova Applications that must communicate with the Application Server.

SecureLogical

Whether or not the cookie should be transmitted over a secure connection (HTTPS).

ValueCharacter

Cookie value.

Description

Adds a cookie to the response's cookie collection.

Discussion

Context.Response.AppendCookie(Cookie) adds a cookie to the response's cookie collection. If a cookie with the same name already exists, it's replaced with the new cookie.

dim cookieName as c = "MyCookieName"
dim newCookie as System::Web::HttpCookie = new System::Web::HttpCookie(cookieName)
newCookie.HttpOnly = .t.
newCookie.Value = "This is the cookie's value."
Context.Response.AppendCookie(newCookie)

Example: Setting the SameSite attribute

dim cookie as System::Web::HttpCookie = new System::Web::HttpCookie("MyCookie", "MyCookieValue")
cookie.HttpOnly = .t.
cookie.SameSite = System::Web::SameSiteMode::None
Context.Response.AppendCookie(cookie)

Example: Deleting a Cookie

You cannot directly delete a cookie on a user's computer. To remove a cookie, you must set the cookie's expiration date and time to a time in the past. The browser will then remove the cookie the next time a request is made and the cookie is inspected.

dim cookieName as c
cookieName = "CustomCookie"
if Context.Request.HasCookie(cookieName) then
    dim cookie as System::Web::HttpCookie = Context.Request.GetCookie(cookieName)
    cookie.HttpOnly = .t.
    cookie.Expires = ctodt("1/1/1970 12:00:00 00 am")
    Context.Response.AppendCookie(cookie)
end if

See Also