Cookies - Write, Read, Delete, Test for - November 2008
Updated September 2015

November 2008 - This months tip shows how to write, read, delete and test for cookies using VB.Net. The properties and methods are encapsulated in class module cCookie.vb. See below to download the source code.

September 2015 - I wrote a demo app that shows how to use the code. Click here to link to the demo app.

HttpOnly - In August 2015, one of my clients suggested, for security reasons, that the cookie property HttpOnly should be set to True. So now by default that property is set when a cookie is written using these routines. You can override this setting - see below.

The Demo App - Click here to run the demo app.

Test - Functions TestCookieWrite and TestCookieRead can be used to test if the client's browser supports cookies. Upon startup the demo app tests the browser and reports results.

Action - Click one of the action buttons to Write, Read or Delete a cookie. Enter the cookie's name and if writing one enter the value to be assigned to the named cookie.

Methods

CookieWrite - Use this method to write a cookie. Returns True is successful. For example:

Dim oC As New cCookie
If oC.CookieWrite("Action", "Edit") Then

CookieRead - Use this method to read a cookie. Returns True if successful. Property sValue contains the returned value. For example:

Dim oC As New cCookie
Dim sValue As String = ""
If oC.CookieRead("Action") Then
    sValue = oC.sValue
End If

CookieDelete - Use this method to delete a cookie. Returns True is successful. For example:

Dim oC As New cCookie
If oC.CookieDelete("Action") Then

TestCookieWrite - Use this method to write a test cookie. Cookie 'cookietest' is written with a 'True' value. For example:

Dim oC As New cCookie
If oC.TestCookieWrite() Then

TestCookieRead - Use this method to write then read a test cookie. Returns True is successful. For example:

Dim oC As New cCookie
If oC.TestCookieRead() Then

Properties

sName - The cookie's name, for example: "Action"

sValue - The cookie's value, for example: "Edit"

iExpires - The cookie's expiration in integer days from today, for example: 7. Default is: 365 [one year].

The above properties can be set or passed as variables.

bHttpOnly - If checked then cookie property HttpOnly is set to Yes.

sMessage - If not empty then it contains a passed message. For example: * Required Data Missing

Overloads

The methods are overloaded. The overloads:

Method Parameters
CookieWrite  
CookieWrite sName
CookieWrite sName, sValue
CookieWrite sName, sValue, iExpires
CookieRead  
CookieRead sName
CookieDelete  
CookieDelete sName

Download VB.Net Code

Click here to download the VS2008 VB.Net source code [244k].

Previous Tip   Tip Index   Next Tip

Home

Updated 2016/12/23