When a user visits your website, you can use cookies to store user login (storing a password is unsecure!) or user preferences or other information. When the user visits your Web site another time, the application can retrieve the information it stored earlier.
This sample shows how to save cookie and how to retrive it when you visit site another time.
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["MyCookie"] != null)
txtSavedCookie.Text = "Cookie is: " + Request.Cookies["MyCookie"].Value;
else
txtSavedCookie.Text = "No cookies!";
}
protected void btnSaveCookie_Click(object sender, EventArgs e)
{
HttpCookie cookie = new HttpCookie("MyCookie", txtCookieValue.Text);
Response.AppendCookie(cookie);
txtResult.Text = "Cookie saved!! Close this site and open it again - you will see saved cookie value at the top of this page!";
}
All our simple samples available as Microsoft Visual Studio 2008 website projects.
You can DOWNLOAD this Save cookie sample project from Rapidshare.