Create a cookie and read it back in C#

Been a bit bare at the mo, lots of dev work going on so thought I would fill it with a simple how to with Cookies.

To create a cookie on your site...

 
// Lets check if it exists or not yet.
if (Request.Cookies["MySite_LastVisit"] == null)
{
 
// Create new Cookie object to store users last visit to the site.
HttpCookie ckUserLastVisit = new HttpCookie("MySite_LastVisit");
 
// Get current date/time
DateTime dtNow = DateTime.Now;
 
// Set the cookie value.
ckUserLastVisit.Value = dtNow.ToString();
 
// Set the cookie expiration date. We will say one day from now
// but you could say as long as you want.
ckUserLastVisit.Expires = dtNow.AddDays(1);
 
// Add the cookie.
Response.Cookies.Add(ckUserLastVisit);
 
}

So that is it created. Lets see how to read that information back.

 
if (Request.Cookies["MySite_LastVisit"] == null)
{
// Just assign our value to a new string and then do what we want with it.
String sLastVisitDate = Request.Cookies["MySite_LastVisit"].Value;
}
 

That should be it, nice and simple.