C# - The HttpCookie Class

The HttpCookie class is under System.Web namespace. It contains properties and methods necessary to work with individual cookies.

Properties of the HttpCookie Class

Item Description
Domain Contains the domain of the cookie
Expires Contains the expiration time of the cookie
HasKeys Contains True if the cookie has subkeys
Name Contains the name of the cookie
Path Contains the virtual path to submit with the cookie
Secure Contains True if the cookie is to be passed in a secure connection only
Value Contains the value of the cookie
Values Contains a collection of all cookie values

Example of how to work with Cookies

<% @Page Language="C#" %>
<%@ Import Namespace="System.Web" %>

<html>
<head>
   <script language="C#" runat="server">
      void Page_Load(Object Source, EventArgs E){
          if(Page.IsPostBack){
               //Create new cookie
               HttpCookie newCookie = new HttpCookie("UserName");

               //Configure cookie
               newCookie.Domain = "yoursite.com";
               newCookie.Expires = new DateTime(2006, 12, 1);
               newCookie.Name = "UserName";
               newCookie.Path = "/";
               newCookie.Secure = false;
               newCookie.Value = txtUsername.Text;

               //Add cookie to response object
               Response.Cookies.Add(newCookie);

               //Output cookie value to page
               msg.Text = Response.Cookies["Username"].Value;
          }
      }
   </script>
</head>
<body>
<h1>Working with Cookies</h1>
<form runat="server" id="form1" name="form1">
   Cookie contents: <asp:label id="msg" runat="server"></asp:label><br>
   <asp:TextBox id="txtUsername" runat="server"></asp:TextBox>
   <input type="submit" runat="server">
</form>
</body>
</html>
HttpCookie.Domain

Syntax

String Domain

Description

The Domain property is a string containing the domain of the cookie.

Example

Response.Cookies["myNameCookie"].Domain = "triconsole.com";
HttpCookie.Expires

Syntax

DateTime Expires

Description

The Expires property is used to specify a date at which a cookie becomes invalid.

Example

DateTime toExpire = new DateTime(2006, 12, 1);
Response.Cookies["myNewCookie"].Expires = toExpire;
HttpCookie.HasKeys

Syntax

Boolean HasKeys

Description

The HasKeys property is True if the cookie has subkeys.

Example

Request.Cookies["myNewCookie"].HasKeys;
HttpCookie.Name

Syntax

String Name

Description

The Name property contains the name of the cookie.

Example

string cookieName = Request.Cookies["myNewCookie"].Name;
HttpCookie.Path

Syntax

String Path

Description

The Path property contains the virtual path associated with the current cookie.

Example

string cookiePath = Request.Cookies["myNewCookie"].Path;
HttpCookie.Secure

Syntax

Boolean Secure

Description

The Secure property is True if the cookie should be sent only over a secure connection.

Example

Boolean transmitSecure = Request.Cookies["myNewCookie"].Secure;
HttpCookie.Value

Syntax

String Value

Description

The Value property contains the actual text value of the cookie.

Example

Response.Cookies["myNewCookie"].Value = "Test Value";
HttpCookie.Values

Syntax

NameValueCollection Values

Description

The Values property is a collection of names-value pairs that enable a cookie to contain subvalues.

Example

Response.Cookies["myNewCookie"].Values["SubCookie1"] = "value1";
Response.Cookies["myNewCookie"].Values["SubCookie2"] = "value2";


string myResponse = Request.Cookies["myNewCookie"].Values["SubCookie1"] + " ";
myResponse += Request.Cookies["myNewCookie"].Values["SubCookie2"];