aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net Cookies - Read / Get / Create Cookies in Website using C#, VB.NET

May 29, 2016

Here I will explain how to read cookies in using c#, with example or create or get cookie value from website using c#, with example or check if cookie exists or not in website using c#, with example. By using “Request.Cookies” properties we can get or read cookie value from website using c#,

To create, read or get cookies in open your aspx page and write the following code

<html xmlns="">
<head runat="server">
<title>Create, Write and Read Cookies in using C#, VB.NET</title>
<form id="form1" runat="server">
<asp:Button ID="btnCreate" runat="server" Text="Create Cookies" onclick="btnCreate_Click" />
<asp:Button ID="btnGet" runat="server" Text ="Get Cookie Values" onclick="btnGet_Click" /><br /><br />
<asp:Label ID="lblResult" runat="server"/>
After completion of aspx page add following namespaces in codebehind

C# Code

using System;
using System.Web;

After completion of adding namespaces you need to write the code like as shown below

protected void btnCreate_Click(object sender, EventArgs e)
Response.Cookies["UserId"].Value = "1234";
Response.Cookies["UserId"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["UserName"].Value = "Suresh";
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
lblResult.Text = "Cookie Values Created Successfully";
protected void btnGet_Click(object sender, EventArgs e)
string userid = Request.Cookies["UserId"].Value;
string username = Request.Cookies["UserName"].Value;
lblResult.Text = "<b>Cookie Values</b> : UserId: " + userid + ", UserName: " + username;


Imports System.Web

Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Protected Sub btnCreate_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Cookies("UserId").Value = "1234"
Response.Cookies("UserId").Expires = DateTime.Now.AddDays(1)
Response.Cookies("UserName").Value = "Suresh"
Response.Cookies("UserName").Expires = DateTime.Now.AddDays(1)
lblResult.Text = "Cookie Values Created Successfully"
End Sub
Protected Sub btnGet_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim userid As String = Request.Cookies("UserId").Value
Dim username As String = Request.Cookies("UserName").Value
lblResult.Text = Convert.ToString((Convert.ToString("<b>Cookie Values</b> : UserId: ") & userid) + ", UserName: ") & username
End Sub
End Class


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


smith said...

thanks for updating with code examples..

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.