Thursday, January 3, 2013

Create/Retrieve Cookie in ASP.NET

Below example show you how to,

Check cookies is null
Create new cookies
Read cookie

string computerID = string.Empty; //string variable

if (Request.Cookies["webSiteUnique"] == null) // check cookie is null
 HttpCookie uniq = new HttpCookie("webSiteUnique"); // create new cookie named "webSiteUnique"
 computerID = "1"; 
 uniq.Value = computerID; // set cookie value
 Response.Cookies.Add(uniq); // add newly created cookie to the list of cookies
 computerID = Request.Cookies["webSiteUnique"].Value; //read cookie value

No comments:
Write comments
Recommended Posts × +